It has already been mentioned that
foreach (@_) { $number += @_[$_]; }
should be
foreach (@_) { $number += $_; }
If you wanted $_ to have the indexes, you'd use
for (0..$#_) { $number += $_[$_]; }
Another way is to use highly efficient sum from the core module List::Util.
use List::Util qw( sum ); my @fred = qw{ 1 3 5 7 9 }; my $fred_total = sum @fred; my $line = <STDIN>; my @nums = split ' ', $line; my $user_total = sum @nums;
In reply to Re: Having problems with addition in sub
by ikegami
in thread Having problems with addition in sub
by bluethundr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |