Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by nuance (Hermit)
on Aug 17, 2000 at 19:42 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-28 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found