Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: what's this loo code? (*slurp*)

by tye (Sage)
on Feb 04, 2003 at 17:58 UTC ( [id://232609]=note: print w/replies, xml ) Need Help??


in reply to what's this loo code?

I waited and two explanations have already appeared as I hoped and expected...

But I wanted to mention my first impression when I saw that code in jmcnamara's node: "Oh, that takes way too much memory and will fail for files that don't fit within your available swap space".

The number of possible Perl one-liners that print a line count and don't suffer from requiring way too much memory for large files is quite huge. I'll shoot for a very straight-forward one:    perl -le '1 while <>; print $.' file I'm almost certain you can make that faster or shorter (without reintroducing the memory hog aspect of it), but I really don't care to do either.

                - tye

Replies are listed 'Best First'.
Re: Re: what's this loo code? (*slurp*)
by Kanji (Parson) on Feb 04, 2003 at 18:51 UTC
    I'm almost certain you can make that faster or shorter

    I can't imagine it making a significant dent in speed for small(er) files, but -l (sh|c)ould be omitted.

    Perhaps...

    perl -ne 'END{ print $., $/ }' file

    ...or...

    perl -ne 'END{ print $. }' file

        --k.


Re: Re: what's this loo code? (*slurp*)
by jmcnamara (Monsignor) on Feb 04, 2003 at 23:58 UTC

    But I wanted to mention my first impression when I saw that code in jmcnamara's node: "Oh, that takes way too much memory and will fail for files that don't fit within your available swap space".

    I hope you realise that I was aware of that. :-)

    This snippet's only real value is as a mild curiosity. When I first posted it here I wrote: "There are many ways of doing this and most of them are better".

    If I really wanted a line count I would use one of the following in this order of preference:

    wc -l file awk 'END{print NR}' file perl -nle 'END{print $.}' file
    --
    John.

Re^2: what's this loo code? (*slurp*)
by Aristotle (Chancellor) on Feb 04, 2003 at 23:40 UTC
    Efficient:
    perl -ne'BEGIN{$/=\65536} $a+=tr/\n//; END{print"$a\n"}' file
    Pity there's no way to set $/ to a reference to a number using a switch.

    Makeshifts last the longest.

      If the last line doesn't end with a \n (that is, \n is not the last character in the last chunk) you'll be off by one.
Re: Re: what's this loo code? (*slurp*)
by John M. Dlugosz (Monsignor) on Feb 05, 2003 at 03:52 UTC
    I guess the comments throw off my expectations, too. Since when does "wc" stand for "number of lines in a file"? The dictionary says it's a British toilet (a.k.a. "the loo") and it could be an acronym for word count, so I was trying to figure out how that was counting words.

      The wc utility counts words, lines, and characters in a file. wc -l tells wc to only display the number of lines.

      --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-03-19 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found