Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

@array elements multiplication with another array elements.

by New Perlmonk (Initiate)
on Oct 30, 2012 at 16:19 UTC ( [id://1001525]=perlquestion: print w/replies, xml ) Need Help??

New Perlmonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks and My Mentors, I have two arrays @array1 and @array2 containing scalar variables. I would like to multiply the array elements of @array1 with @array2 elements. Ex: @array1 = (3,4,6,5,8) @array2 = (2,2,4,5,1) after multiplication I want to see (6,8,24,25,8) please help here to play with elements of arrays.

Replies are listed 'Best First'.
Re: @array elements multiplication with another array elements.
by tobyink (Canon) on Oct 30, 2012 at 16:39 UTC
    my @array1 = (3,4,6,5,8); my @array2 = (2,2,4,5,1); my @mults = map { $array1[$_] * $array2[$_] } 0..$#array1; print "@mults\n";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: @array elements multiplication with another array elements.
by tobyink (Canon) on Oct 30, 2012 at 20:27 UTC

    Here's another example using pairwise from the lovely List::MoreUtils package...

    use List::MoreUtils qw( pairwise ); my @array1 = (3,4,6,5,8); my @array2 = (2,2,4,5,1); my @mults = pairwise { $a * $b } @array1, @array2; print "@mults\n";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Wow, how can pairwise tell where the end of 1st array is? There was always the problem feature that regardless whether you write
      my @array1 = (3,4,6,5,8); my @array2 = (2,2,4,5,1);
      or
      my @array1 = (3,4,6,5,8,2,2,4); my @array2 = (5,1);
      using it with coma always results in flattened list:
      @array1,@array2
      is equivalent to
      (3,4,6,5,8,2,2,4,5,1)
      and yet pairwise deals with it:
      c:\>perl -le "use List::MoreUtils qw/pairwise/; @a=(1..5);@b=(11..15); + print for pairwise {$a+$b} @a, @b" 12 14 16 18 20 c:\>perl -le "use List::MoreUtils qw/pairwise/; @a=(1..5, 11,12);@b=(1 +3..15); print for pairwise {$a+$b} @a, @b" 14 16 18 4 5 11 12

        It uses a prototype of &\@\@. That means the two arrays get implicitly turned into arrayrefs by the Perl parser. The body of the sub just sees a coderef and two arrayrefs - not a flattened list. See perlsub.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: @array elements multiplication with another array elements.
by roboticus (Chancellor) on Oct 30, 2012 at 16:25 UTC

    New Perlmonk:

    Where are you getting stuck?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: @array elements multiplication with another array elements.
by marquezc329 (Scribe) on Oct 30, 2012 at 17:18 UTC
Re: @array elements multiplication with another array elements.
by Pizentios (Scribe) on Oct 30, 2012 at 18:37 UTC
    use warnings; use strict; my @array1 = (3,4,6,5,8); my @array2 = (2,2,4,5,1); my $num = scalar @array1; my @answers; for (my $x = 0; $x < $num; $x++) { push(@answers, $array1[$x]*$array2[$x]); }

      Or use a for ( or foreach ) loop like so:

      my @answers; for my $x ( 0 .. $#array1 ) { push @answers, $array1[$x] * $array2[$x]; }

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
Re: @array elements multiplication with another array elements.
by Don Coyote (Hermit) on Nov 03, 2012 at 15:57 UTC

    Point to note, the for loop uses the construct of scalar(@arr) and the map operation uses the special variable $# (+ arrayname) $#arr to determine the length of the list being implemented within the operations.

    where @arr = (1,2,3,4)

    scalar(@arr) returns the number of items in the list scalar(@arr) = 4

    $#arr returns a zero indexed length of items list $#arr = 3

    $#arr returns the highest index of the array $#arr = 3

    The C style for loop has the condition of the increment being lower than scalar(@arr) where;

    scalar(@arr) = 4, ( $i = 0, $i < scalar(@arr), $i++ ) = 0,1,2,3

    The perl map operation however, inserts the incremented counter for the $_ variable where;

    $#arr = 3, 0..$#arr = 0,1,2,3

    updates to definition of $# variable as result of reply from Anomolous.

    Should the $ARRAY_BASE special variable $[ be set to other than the default of 0. The $#arr may return an offset to the zero indexed result that you are probably not expecting. Generally use of this variable is frowned upon at best. However.. dah dih dah dih.


    my @arr = split //,'Coyote'; print map {$arr[$_]} 0..$#arr;
      $#arr returns a zero indexed length of items list $#arr = 3

      I think it is fundamentally misleading to refer to what  $#array returns as a "length". Rather, I would say it is the highest index of the array, and depends on what the $ARRAY_BASE special variable (aka $[ – see perlvar) has been set to (but don't do that!).

        Thanks Anomolous, point noted. I should also reflect that it is not truly the map operator that is using the $#arr construct to obtain the highest array index as such, rather it is being used by the .. range operator. As map expects a list of some description to work with after the modifiying operation. In this case we give the list in scalar context of a range, much like the for iterator, so perl goes ah yah, this is some funky for loop right, and cooly proceeds.

        I will now look into how the $ARRAY_BASE special variable affects the $# variable. I would not have thought a difference would be made, being as the highest index of an array is always going to be the highest index? (unless $[ is set higher perhaps... hmm...).

        *Footsteps echo quietly away as Coyote trundles off into the dark echoing corridors of the echosome vaults.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1001525]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found