Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: How do I find the total number of lines in a file?

by nuance (Hermit)
on Aug 17, 2000 at 19:42 UTC ( #28306=note: print w/replies, xml ) Need Help??

in reply to How do I find the total number of lines in a file?

The variable $. contains the line number of the last file read from, so:
for (<FILEHANDLE>); print "FILEHANDLE contains $. lines";
note: If you're using <> to iterate over @ARGV then the line numbers don't reset over the files. So on if you were reading five ten line files on line five of file three, $. will contain 25.

See perldoc perlvar for more info

Editor's Note:
$. is reset on an explicit close. Opening an open filehandle first does an implicit close on it, then opens it. This does NOT reset $. to zero. This is one of the many reasons to explicitly close filehandles when you are done with them. <> Uses the default filehandle and does not explicitly close it, which is why you get the behaviour that nuance mentioned. Of course, you can also set $. yourself as a point to start counting from (although this does NOT tell the read operator to start at the specified line, $. is just a counter) and you can scope $. with local $. as well.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-01-19 02:21 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (55 votes). Check out past polls.