Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by duelafn (Parson)
on May 20, 2017 at 21:44 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190766]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found