Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Golf: Length of longest line in a file

by toolic (Bishop)
on Jul 02, 2010 at 16:01 UTC ( #847747=perlmeditation: print w/replies, xml ) Need Help??

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

Please show me how to slim this down.

Replies are listed 'Best First'.
Re: Golf: Length of longest line in a file
by Anonymous Monk on Jul 02, 2010 at 17:49 UTC
    You could cheat I guess:

    $ wc -L file

      ++ That's a good way of cheating, especially since I learned a new usage of wc today. That eluded my quick google search earlier today, which only seemed to drum up awk solutions.
      wc will count tabs (\t) as 8 characters and newlines (\n) as 0 though,
      don't know if it supposed to be like that, but it does:
      $ hexdump test 0000000 0a09 0000002 $ wc -cm test 2 2 test $ wc -L test 8 test $
Re: Golf: Length of longest line in a file
by BrowserUk (Pope) on Jul 02, 2010 at 19:54 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

        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.
Re: Golf: Length of longest line in a file
by salva (Canon) on Jul 02, 2010 at 19:20 UTC
Re: Golf: Length of longest line in a file
by Fletch (Chancellor) on Jul 02, 2010 at 20:40 UTC

    Meh, everyone's beat mine.

    #!/usr/bin/runhaskell import IO main=do t<-(hGetContents stdin);putStrLn$show$foldl max 0$map length$l +ines t

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      well, you could have used liftM and merge map length in the fold function

      liftM (foldl (\x y -> max x $ length y) 0 . lines) $hGetContents stdin

      oh.. wait.. what is this?

      Wouldn't getContents from the prelude work instead of IO.getContents? This shortens a lot because now you can use ghc -e instead of runhaskell, eg.

      #!/bin/sh ghc -e 'getContents>> length.lines'

      We also use some other functions of the Prelude for brevity. Note that the code can't be in the same argument as -e.

Re: Golf: Length of longest line in a file
by moritz (Cardinal) on Jul 04, 2010 at 16:06 UTC
    I kinda like how Perl 6 code with few spaces comes in golfing distance, without the need for any ugly tricks:

    So that's 24 chars without the quotes, and no fancy command line options used .

    Perl 6 - links to (nearly) everything that is Perl 6.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://847747]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-08-04 23:51 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (44 votes). Check out past polls.