Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: John Guttag's book - 2nd exercise. My attempt in Perl.

by duelafn (Vicar)
on May 20, 2017 at 21:44 UTC ( #1190766=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190766]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2021-02-24 22:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?