in reply to John Guttag's book - 2nd exercise. My attempt in Perl.
Update: As pointed out by AnoMonk below, these while loops have a bug when one of the array item is 0.I suggest a better for loop in another post below.use strict; use warnings; my @array = (-11,-13,4, 22, 17); print find_max_odd(@array), "\n"; sub find_max_odd { my $max; while ($max = shift) { last if $max % 2; } return "Not found" unless @array; while (my $current = shift) { next unless $current % 2; if ($current > $max) { $max = $current; } } return $max; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: John Guttag's book - 2nd exercise. My attempt in Perl.
by KurtZ (Friar) on May 20, 2017 at 22:12 UTC | |
by Laurent_R (Canon) on May 21, 2017 at 09:27 UTC | |
|
Re^2: John Guttag's book - 2nd exercise. My attempt in Perl.
by Anonymous Monk on May 22, 2017 at 18:03 UTC | |
by Laurent_R (Canon) on May 22, 2017 at 19:19 UTC |