Ignorance

Sometimes you wonder how you didn't know something for so long. In this case, it is using a conditional expression in conjunction with a for loop.

perl -e '$mx = $_ > $mx ? $_ : $mx for ( 1, 6, 2, 3, 5, 4 ); print "$mx\n"'

3 comments:

Matt Caron said...

Ew. Ternary operator and weird perl for construction... are you competing for an inscrutable perl competition or something? I'd flag this in code review on at least two violations...

Andrew Gilmartin said...

I don't generally write idiomatic Perl in scripts, but I when working at the command line creating ah hock one-time use script then it is idiomatic all the way! Why Perl does not have a max() and min() function within its core is a mystery to me. I need this far more often than, for example, http://perldoc.perl.org/functions/ucfirst.html. Ditto for the missing reduce() to go along with the existing map().

Matt Caron said...

I guess I've been burned by that too many times. Basically, if it's worth figuring it out once, then I'll likely need it again, so it's worth formatting correctly and putting it in to a "kludge scripts" repo, just to save brainpower in the future.

And I'm with you on missing max() and min().. you could always add them via a module, however. :-)