in reply to How To Count Lines In File?

perl -lpe '}{*_=*.}{' file

Abigail

Replies are listed 'Best First'.
Re: Re: How To Count Lines In File?
by John M. Dlugosz (Monsignor) on Jan 27, 2003 at 06:10 UTC
    Very interesting!

    So, the meaning of -p is performed by textually including the loop around the actual code, so beginning with "}" will close the -p's while loop, and the final "{" will ballance the closing brace at the end of the expansion.

    I never thought about that. I always just figured that built-in looping construct was done on a syntactic boundary, like:

    while (<>) { eval $option_e; print }
    only done in the parse-tree level, not by simply generating more source text!

    So... that's not documented or to be relied on, right?

    —John

      `perl --help' or `perldoc perlrun'


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        Which states, “causes Perl to assume the following loop around your program”. It doesn't say that this is done by pasting text before parsing it. I always supposed it was done deeper in the interpreter, after parsing.
Re: Re: How To Count Lines In File?
by OM_Zen (Scribe) on Jan 27, 2003 at 02:45 UTC
    Hi ,

    The code you have here, does it work only in Unix , I got the contents of the file in WINDOWS instead of showing the number of lines.Please explain if this is for Unix alone or why in windows it acts this way
      The command shell in windows can't handle the single quotes (') shown in that oneliner. Change them to double quotes (") and it should work for you.
      perl -lpe "}{*_=*.}{" file


      ryddler
(jeffa) 2Re: How To Count Lines In File?
by jeffa (Bishop) on Jan 27, 2003 at 15:27 UTC
    Is there any gain in using globs over the variables themselves, such as:
    perl -lpe '}{$_=$.' file
    ?

    On another similar note, last semester my C++ students had to write a small C++ program that averages numbers from a flatfile (one per line). I wowed them with this little doosie:

    perl -lpe '$s+=$_}{$_=$s/$.' file
    UPDATE
    That's a fair cop! :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Is there any gain in using globs over the variables themselves

      If you can answer the question "is there any gain in using perl -ple '}{*_=*.}{' file over wc -l file", you can figure out the other question yourself.

      Abigail

        not all systems have wc - now what about the globs?