Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:51+00:00 2026-06-17T09:13:51+00:00

Suppose I have the following two equal-sized arrays in my perl program: my @arr1

  • 0

Suppose I have the following two equal-sized arrays in my perl program:

my @arr1 = 1..5;
my @arr2 = 6..10;

I’m trying to get their dot-product using the reduce function defined in the List::Util core module but the following is not working for me:

my $dot_prod = reduce { $arr1[$a] * $arr2[$a] + $arr1[$b] * $arr2[$b] }0..$#arr1;

I get 50 as my output instead of the expected 130.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:13:52+00:00Added an answer on June 17, 2026 at 9:13 am

    The documentation describes the behavior of reduce as follows:

    The first call will be with $a and $b set to the first two elements of
    the list, subsequent calls will be done by setting $a to the result of
    the previous call and $b to the next element in the list.

    Thus, in this case, on the first iteration reduce will set $a = 0 and $b = 1, and hence, execute

    $arr1[0] * $arr2[0] + $arr1[1] * $arr2[1] 
    

    This temporary result happens to be 20.

    Now, for the second iteration, $a is set to the result of the previous iteratrion and so $a = 20 and $b = 2. Thus, the following will be executed

    $arr1[20] * $arr2[20] + $arr1[2] * $arr2[2]
    

    which is not what we want.

    A possible workaround:

    prepend an initial 0 to the list provided as input to reduce as follows:

    my $dot_prod = reduce { $a + $arr1[$b] * $arr2[$b] } 0, 0..$#arr1;       
    

    This gives us the desired result since on the first iteration $a = $b = 0 and we’ll compute

    0 + $arr[0] * $arr[0]
    

    whose result will be 6.

    Then in the second iteration, we’ll have $a = 6 $b = 1 and so we’ll compute

    6 + $arr1[1] * $arr2[1]
    

    etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
For example suppose I have the following two functions function a(param1) { console.log(param1); }
suppose I have following test/ Main.py test/one/ One.py test1.txt test/two/ Two.py test2.txt What I
Suppose I have two tables Main and Other like the following: Main +----------+-----------------------------------+ |
Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Suppose you have the following (ill-formed) program: struct A { A(int, int) { }
While designing a C++ class, suppose I have following two options: Option 1: One
This is a scjp mock exam question. Suppose I have the following two files:
Suppose I have the following in my template: [%- pages = [ 'one', 'two',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.