Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Adding two lists together

by markkawika (Monk)
on Nov 04, 2009 at 23:59 UTC ( [id://805101]=perlquestion: print w/replies, xml ) Need Help??

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

What's a simple way to add the elements of two arrays together? Something like this:

$list1[0] += $list2[0]; $list1[1] += $list2[1]; $list1[2] += $list2[2]; $list1[3] += $list2[3];

The solution that occurs to me is:

for my $i (0..3) { $list1[$i] += $list2[$i]; }

What if instead of an array, you have a list (or, at least, you could make a list)? Something like this:

$some_value += $array[0]; $some_other_value += $array[1]; $this_variable += $array[2]; $that_variable += $array[3];

That doesn't seem to lend itself as handily to a simple for loop.

Replies are listed 'Best First'.
Re: Adding two lists together
by jwkrahn (Abbot) on Nov 05, 2009 at 00:55 UTC
    use List::MoreUtils 'pairwise'; pairwise { $a += $b } @list1, @list2;
      ... and to suppress the annoying warnings:
      use strict; use warnings; use Data::Dumper; use List::MoreUtils qw(pairwise); my @list1 = (1 .. 5); my @list2 = (11 .. 15); { no warnings; pairwise { $a += $b } @list1, @list2; } print Dumper(\@list1); __END__ $VAR1 = [ 12, 14, 16, 18, 20 ];
      List::MoreUtils
Re: Adding two lists together
by shmem (Chancellor) on Nov 05, 2009 at 00:44 UTC

    You could use a statement modifier:

    $list1[$_] += $list2[$_] for 0..$#list1;

    Assignment to a list of variables:

    ($some_value, $some_other_value, $this_var, $that_var) = @array;
Re: Adding two lists together
by colwellj (Monk) on Nov 05, 2009 at 00:13 UTC
    Mark,

    For the 2 lists try this to always get every item. This assumes that the list2 has at least as many elements as list one though
    for my $i(0..$#list1){ $list1[$i] += $list2[$i]; }
    For the non list how about;
    my @list2; push @list2, $some_value, $some_other_value, $this_variable, $that_var +iable; for my $i(0..$#list1){ $list1[$i] += $list2[$i]; }
    I'm not sure if its really worth it however.

    John
      Another approach to the pesky 'list of scalars':
      >perl -wMstrict -le "use List::MoreUtils qw(each_array); my ($w, $x, $y, $z) = ( 1, 2, 3, 4); my @incs = (10, 20, 30, 40); my $ea_inc = each_array @incs; $_ += $ea_inc->() for $w, $x, $y, $z; print qq{w $w x $x y $y z $z}; " w 11 x 22 y 33 z 44
      Note that nothing is done here to check that the list and the array have the same number of elements!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-09-07 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.