Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Golf: Length of longest line in a file

by eyepopslikeamosquito (Canon)
on Jul 04, 2010 at 07:03 UTC ( #847958=note: print w/ replies, xml ) Need Help??


in reply to Re: Golf: Length of longest line in a file
in thread Golf: Length of longest line in a file

This one gives the same result as BrowserUk's 24 but is two strokes less:

perl -nE "$s^=$_}{say length$s" perl -lpe "\$Z[y///c]}{$_=@Z" perl -nE "\$Z[y///c]}{say$#Z" # update: alternative 22
Instead of finding the maximum length by exploiting Perl's wonderful bitwise string operator, we instead exploit Perl's marvellous autovivification (exploiting autovivification is common in golf, as was done by Jasper in The golf course looks great, my swing feels good, I like my chances (Part V), for example).

For golf historians, notice that the use of y///c instead of length is known as Abigail's length horror but was actually first invented by Joseph N Hall in 1996. And the use of a (one stroke) reference \ to trigger autovivification is known as Thelen's Device and was invented by Michael Thelen aka thelenm in 2002. This history is discussed in The Lighter Side of Perl Culture (Part IV): Golf.


Comment on Re^2: Golf: Length of longest line in a file
Select or Download Code
Re^3: Golf: Length of longest line in a file
by ikegami (Pope) on Jul 04, 2010 at 18:39 UTC

    Three (two) less for cmd.

    perl -nE"$s^=$_}{say length$s" # Switch quotes for bash perl -nE "\$Z[y///c]}{say$#Z" # Switch quotes for bash perl -nE"\$Z[y///c]}{say$#Z" # Switch quotes for bash perl -nE\$Z[y///c]}{say$#Z # cmd specific 1234567890123456789012345

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-04-18 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls