4 ms·
This is why I like Perl, perl -e 'print sort{length $a <=> length $b} <>' english-words.txt > sorted.txt
by drucken 14y ago
This is why I like Perl,
perl -e 'print sort{length $a <=> length $b} <>' english-words.txt > sorted.txt
- emillon 14y agoThanks ! Just, you're missing a "print" and single quotes would work better :)
- drucken 14y agoCheers for that! Fixed.
- rane 14y agoLooks handy indeed, I wasn't aware perl had such potential. Can you explain what's happening there?
- Leynos 14y agoI love that I'm always being shown quicker ways of doing this. Using Perl for this wouldn't have occurred to me, so I'm pleased I saw this today. What it's doing: One of the call types of the sort function takes a block as its fist parameter and a list as its second. When called this way, the block should operate on two special variables ($a and $b), returning a -1, 0 or 1 depending upon whether $a should be placed before, beside or after $b in the sorted list. The "<=>" operator does this comparison on numbers, the numbers in question being the lengths of the two variables. The second parameter here is a read of a file handle (the syntax for this is "<filehandlename>"). When the file handle read is used with no named file handle, it reads the the contents of each parameter the script was passed in turn (or STDIN if none). Perl reads files as lists of strings, with each string being a line in the file. The sorted list is then printed to STDOUT.
- deleted 14y ago[deleted]
- cabbeer 14y agodo you mind sharing the output?