Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Count number of lines in a text file

by Anonymous Monk
on Jan 23, 2013 at 08:53 UTC ( #1014861=note: print w/replies, xml ) Need Help??


in reply to Count number of lines in a text file

Everybody should be aware that in an ASCII file, the last line may NOT contain a '\n' character. This means the following code:
while (sysread FILE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); }
will never count the last line! Please do not use the above code !!!

Use:

while (<FILE>) { $lines++ }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014861]
help
Chatterbox?
[LanX]: ... thought it's a root post
[ww]: That's a [id://1179775 beaut], all right!
[ww]: beaut

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2017-01-17 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (158 votes). Check out past polls.