in reply to Help with code for adding two arrays.
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";
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
|
|---|