in reply to Re: largest number inside array/hash
in thread largest number inside array/hash
That's the classic max (or min) implementation error. What if all numbers are negative?
$ perl -e'@a=qw(-1 -2 -3 -4 -5); $max=0; printf "Answer allegedly is %d\n",map { $max=($max < $_ ? $ +_ : $max )} @a;' Answer allegedly is 0 $ perl -e'@a=qw(-1 -2 -3 -4 -5); $max=$a[0]; # Or shift @a, if you can be destructive printf "Answer allegedly is %d\n",map { $max=($max < $_ ? $ +_ : $max )} @a;' Answer is -1
|
|---|