Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by marinersk (Priest)
on May 21, 2017 at 04:30 UTC ( [id://1190781]=note: print w/replies, xml ) Need Help??


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

Coded as such:

use strict; use warnings; # Initialize my ($x, $y, $z) = (-11,-13,4); my $maxodd; # Intentionally left as undef # Check $x (though $maxodd will be undef, write it consistently in cas +e code is revectored later) if ($x % 2) { if ( (!defined $maxodd) || ( (defined $maxodd) && ($x > $maxodd) ) + ) { $maxodd = $x; } } # Check $y if ($y % 2) { if ( (!defined $maxodd) || ( (defined $maxodd) && ($y > $maxodd) ) + ) { $maxodd = $y; } } # Check $z if ($z % 2) { if ( (!defined $maxodd) || ( (defined $maxodd) && ($z > $maxodd) ) + ) { $maxodd = $z; } } # Report results if (!defined $maxodd) { print "All are even numbers\n"; } else { print "$maxodd is the greatest odd number\n"; } # Fini exit;

This screams for the use of a subroutine, though I suspect the author isn't at that point in the book yet.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found