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

So I've just started perl, and I can't figure out how to make this code work... Any help would be greatly appreciated. Thank you!
#!/usr/bin/perl #A short script to add the elements of two arrays together. #I highly doubt it is the best way to do this. #I don't know if a perl command exists for this. #This is in the Public Domain #Arrays are declared my $toolong = "seperated by a space "; print "Enter the value of the first array $toolong"; $tdin = <STDIN>; print "Enter the value of the second array $toolong"; $din = <STDIN>; $tdin = chomp($tdin); $din = chomp($din); #Creates arrays and calculates their length. @rray1 = split(/ /, $tdin); @rray2 = split(/ /, $din); $l1 = @rray1; $l2 = @rray2; unless($l1 == $l2) { die(print "ERROR: Arrays are different lengths"); #Error check } #Does the adding for(0..$l1){ push($sums) = $rray1[$_] + $rray2[$_]; } for(0..$l1){ print $sums[$_];}

Replies are listed 'Best First'.
Re: Help with code for adding two arrays.
by davorg (Chancellor) on Jun 19, 2006 at 07:49 UTC

    You've been told about the problems with your usage of push and chomp, but I thought you might be interested in seeing a more "Perlish" way of doing this.

    #!/usr/bin/perl use strict; use warnings; print 'Enter a list of numbers separated by spaces: '; chomp(my $input = <STDIN>); my @arr1 = split /\s+/, $input; my $count = @arr1; print "Enter another $count numbers separated by spaces: "; chomp($input = <STDIN>); my @arr2 = split /\s+/, $input; unless (@arr1 == @arr2) { die "ERROR: Arrays are different lengths\n"; } my @sums = map { $arr1[$_] + $arr2[$_] } 0 .. $#arr1; print "@sums\n";
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Help with code for adding two arrays.
by xdg (Monsignor) on Jun 19, 2006 at 05:53 UTC

    Look at the compilation error:

    Useless use of push with no values at stuff.pl line 29. Type of arg 1 to push must be array (not scalar dereference) at stuff. +pl line 29, near "$sums) " Execution of stuff.pl aborted due to compilation errors.

    Then look perldoc -f push. You need to give it an array, not a scalar:

    push( @sums, $rray1[$_] + $rray2[$_] );

    As a general rule, you might also consider using use warnings and use strict at the top of your scripts. While this was a simple syntax error, those two modules will help you identify other problems you may encounter.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Help with code for adding two arrays.
by davido (Cardinal) on Jun 19, 2006 at 05:50 UTC

    Your use of push is ...incorrect. See the docs for push() and you'll see that it requires more than one argument.

    As for a recommendation on how to add the elements of two arrays, I would do it with List::MoreUtils, using the pairwise() subroutine. Here's from the POD:

    pairwise BLOCK ARRAY1 ARRAY2
    Evaluates BLOCK for each pair of elements in ARRAY1 and ARRAY2 and returns a new list consisting of BLOCK's return values. The two elements are set to $a and $b. Note that those two are aliases to the original value so changing them will modify the input arrays.

    @a = (1 .. 5); @b = (11 .. 15); @x = pairwise { $a + $b } @a, @b; # returns 12, 14, 16, 18, 20

    That's really the right tool for the job, and will lead to clearer code.


    Dave

Re: Help with code for adding two arrays.
by msk_0984 (Friar) on Jun 19, 2006 at 06:27 UTC
    Hi fatalserpent

    Welcome , i just want to say that you should not assign

    the chomp() value to the same variable,

    chomp() just removes the new line character, we just have to use

    $tdin = <STDIN>; chomp($tdin); $din = <STDIN>; chomp($din);
Re: Help with code for adding two arrays.
by madtoperl (Hermit) on Jun 19, 2006 at 06:16 UTC
    Hi fatalserpent,

    Welcome to Monks,The code is having wrong syntax of push and also you have to use $l1 = $#rray1; instead of using $l1 = @rray1;since You are using 0..$l1 in the for loop.Because $l1 = $#rray1 will gives the maximum length minus one of the array size and $l1 = @rray1 will give the maximum size of the array.

    See the below code it is working fine,
    my $toolong = "seperated by a space "; print "Enter the value of the first array $toolong"; $tdin = <STDIN>; print "Enter the value of the second array $toolong"; $din = <STDIN>; chomp($tdin); chomp($din); #Creates arrays and calculates their length. @rray1 = split(/ /, $tdin); @rray2 = split(/ /, $din); $l1 = $#rray1; $l2 = $#rray2; unless($l1 == $l2) { die(print "ERROR: Arrays are different lengths"); #Error check } #Does the adding for(0..$l1){ $sum = $rray1[$_] + $rray2[$_]; push(@sums,$sum) ; } for(0..$l1){ print $sums[$_];}
    see the output Enter the value of the first array seperated by a space 1 3 5 Enter the value of the second array seperated by a space 2 4 6 3711<sumarat@osmosins.wmindia:>
    Thanks and Regards,
    Madtoperl.