Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Golf: Length of longest line in a file

by BrowserUk (Pope)
on Jul 02, 2010 at 19:54 UTC ( #847797=note: print w/replies, xml ) Need Help??

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


perl -nE"$s^=$_}{say length$s"

Replies are listed 'Best First'.
Re^2: Golf: Length of longest line in a file
by eyepopslikeamosquito (Bishop) on Jul 04, 2010 at 07:03 UTC

    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.

      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
Re^2: Golf: Length of longest line in a file (explanation)
by toolic (Bishop) on Jul 04, 2010 at 01:30 UTC

    Please correct me if I am wrong, but the following code ...


    expands to:

    $s = $s ^ $_;

    So, this takes advantage of the string xor operator's ability to increase the shorter string to match the length of the longer string. Then it returns a string whose length is that of the longer of the 2 strings. (Bitwise String Operators)

      Correct. |= works also, but not &=.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2021-09-25 22:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found