http://qs321.pair.com?node_id=1190766


in reply to John Guttag's book - 2nd exercise. My attempt in Perl.

This will do it a bit more nicely (fewer branches) without modifying the variables (and using only if/elsif as requested):

use 5.010; my ($x, $y, $z) = (-11,-13,4); if ($x % 2 and ($x > $y or not $y % 2) and ($x > $z or not $z % 2)) { say "$x is the largest odd"; } elsif ($y % 2 and ($y > $z or not $z % 2)) { say "$y is the largest odd"; } elsif ($z % 2) { say "$z is the largest odd"; } else { say "All numbers are even!"; }

Update: If I were doing this for real, I'd do:

my @nums = (-11, -13, 4); my @candidates = sort { $a <=> $b } grep $_ % 2, @nums; if (@candidates) { say $candidates[-1]; } else { say "They're all even!"; }

Good Day,
    Dean