4 ms·
Favorite Perl snippet (from Joseph Hall's EPP book) [$x => $y]->[$x <= $y] Holy shit - just realized I first touched Perl 15 years ago!
by cliveholloway 13y ago
Favorite Perl snippet (from Joseph Hall's EPP book)
[$x => $y]->[$x <= $y]
Holy shit - just realized I first touched Perl 15 years ago!
- BCM43 13y agoWhat does that do?
- arc_of_descent 13y agoIt prints the greater variable between $x and $y. [$x => $y] creates an array reference. => is just a fancy syntax for a comma. So its actually [$x, $y]. [$x => $y]-> dereferences the array ref. $x <= $y is a boolean expression which return 0 or 1. Accordingly the array index is chosen. Sorry if I'm unable to explain properly! Here's a script. #!/usr/bin/perl use strict; use warnings; my $x = 8; my $y = 9; print [ $x => $y ]->[ $x <= $y ];
- geophile 13y ago- Why not just write ',' instead of '=>'? Is there an actual reason for this other syntax to exist and to be used? - Why does the array have to be explicitly dereferenced? Isn't it obvious from context? - Why count on the mapping from boolean to int being false = 0, true = 1? Pretty much everything other language that has such a mapping does false = 0, true != 0. In one line, a vivid reminder of why I find perl so awful.
- chromatic 13y agoWhy count on the mapping from boolean to int being false = 0, true = 1? Pretty much everything other language that has such a mapping does false = 0, true != 0. I can't wrap my mind around this complaint. "Why should you count on Perl treating booleans the way Perl treats booleans? Other languages don't treat booleans the way Perl treats booleans, so obviously you shouldn't rely on Perl treating booleans the way Perl treats booleans." It's almost as if there were some democratic notion of boolean semantics where every language you know gets one vote.
- drivers99 13y agoThis is obviously written to be clever/cute, not readable. If you just wanted the max of something, you could just write: $x > $y ? $x : $y; (This is the ternary operator borrowed from C, which still is a little confusing.) You could also just use an if / else statement, or write your own min/max functions. Or you can use min() and max() functions imported from List::Util module which can return the min/max from an entire list: http://timmurphy.org/2012/02/01/min-and-max-functions-in-perl/ http://timmurphy.org/2012/02/01/min-and-max-functions-in-per...
- kbenson 13y agoWhy not just write ',' instead of '=>'? Is there an actual reason for this other syntax to exist and to be used? It's a trick so it's easier to remember because while both sections look similar, the operators between them are actually doing different things. i.e. => is a fatarrow (comma), while <= is less-than-or-equal. Replacing> with , results in [ $x , $y ]->[ $x <= $y ]; Which is, arguably, less easy to remember. Why does the array have to be explicitly dereferenced? Isn't it obvious from context? Probably because the first set or brackets is an array reference creation and the second is an array reference subscript? There may be rules that need to be followed for the language to be parseable? Why count on the mapping from boolean to int being false = 0, true = 1? Pretty much everything other language that has such a mapping does false = 0, true != 0. Perl's comparison operators return numerical outputs for true/false, as that's how Perl handles true/false. What value would you expect for true, if there isn't a dedicated type? In one line, a vivid reminder of why I find perl so awful Eh, not my preferred syntax either. I do find that it's possible cool though.
- zoul 13y agoIt’s a highly cryptic way to find the maximum of the two. The first part creates an array (=> is just a syntactic sugar for a comma here) and the second part takes the 0th or 1st item from this array, <= being just regular numeric comparison.
- nicwolff 13y agoThe same thing as ($x, $y)[$x <= $y] but it looks cooler.
- bonemachine 13y agoEgads. If you have time to even think about writing anything other than $x > $y ? $x : $y in this use case, then you have way too much time on your hands.