Excuse my French, but why the **censored** would you store the whole file in memory just to count the lines? Why hardcode the filename in the script? Why bother to open and close the file, when <> is so handy?

Sorry, please forgive the tirade, I don't know what came over me. It must be the ghost of Abigail-II...Certainly TIMTOWTDI. (I find many of my cow-orkers skip over the "gather requirements" phase of programming, and jump headfirst into the shallow end of the implementation pool.)

While playing with this, I wanted to check how similar schemes work. For instance, don't do this either:

perl -e 'print scalar(()=<>),"\n"' filename
I tried this on a 300MB file, which took a long time (I waited several minutes before killing it), lots of memory, and started swapping to disk.

I tried the following on the same 300MB file, which took about 10 seconds, and never went above 2MB memory:

perl -pe "}{$_=$." filename
Inside a script, you could do this:
#!/your/perl/here -p }{$_=$.
(yes, that compiles and runs too) though you may prefer the more conventional
#!/your/perl/here use strict; use warnings; while (<>) {} print "$.\n";
If you want to get fancy, and feed it more than one file at a time, keeping track of each file, try this:
#!/your/perl/here use strict; use warnings; my $file_count = @ARGV; while (<>) {} continue { if (eof) { # print file names for multiple files print "$ARGV: " if ($file_count > 1); print "$.\n"; close ARGV; } }
Someone will ask me for command line arguments to leave off the filenames, and provide summary statistics for multiple files. I'll leave that to OMAR. (Wow, there really is an OMAR! But he doesn't write much :(

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re^2: Count number of lines in a text file by QM
in thread Count number of lines in a text file by Scott7477

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.