Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Count the number of lines in a file

by talexb (Chancellor)
on May 25, 2004 at 17:06 UTC ( [id://356294]=note: print w/replies, xml ) Need Help??


in reply to Count the number of lines in a file

It sounds like to me that you haven't tried any of the things in the Perl book that might answer this question. You do have a Perl book, don't you? I can highly recommend the fine series of books from O'Reilly.

In any event, the variable $. tells you what line you're on, so printing that out after reading through the file would tell you what number the last line of the file was.

#!/usr/bin/perl -w use strict; while(<>){} print "Last line number was $.\n";

If you also want to fine out how many characters there were, you'd add something to count the length of each line:

#!/usr/bin/perl -w use strict; my $totalLength = 0; while(<>){$totalLength += length($_);} print "Last line number was $.\n"; print "Total length was $totalLength\n";

I haven't tested this code; don't know if it works or not. I'll leave that up to you. Play around. See what works. Read that Perl book. Try stuff, and come ask questions when you get stuck, and not before.

Alex / talexb / Toronto

Life is short: get busy!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-03-28 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found