I needed to determine the length of the longest line in a file today.
After overcoming the surprise that I can't recall ever needing to do such a
thing before, I banged out this Perl script:
use warnings; use strict; my $max = 0; while (<>) { my $len = length; $max = $len if $len > $max; } print "$max\n";
Running the code on the following file would print 7. The trailing newline is included in the length of a line.
a abc abcdef abcd abcde
Reflecting on the code over lunch, it occurred to me that that was way too many characters.
Here is my 38:
1 2 3 12345678901234567890123456789012345678
<Reveal this spoiler or all in this thread>
Please show me how to slim this down.
|
---|
Back to
Meditations