Contributed by Anonymous Monk
on Aug 17, 2000 at 19:24 UTC
Q&A
> files
Answer: How do I find the total number of lines in a file? contributed by nuance 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. | Answer: How do I find the total number of lines in a file? contributed by KM From a shell you can:
wc -l file
From Perl you can read the file into an array, then use the array in scalar context to get the number of lines. Or, you could loop through and count, something like:
my $cnt;
open(FH,"file") or die "Damn. $!";
$cnt++ while <FH>;
close FH;
Or, use $. (perldoc perlvar)
while (<FH>) {}
print $.;
Or, to skip blank lines...
open(FH,"calendar.cgi") or die "Damn. $!";
while (<FH>) { $cnt++ if !/^\s+?$/;}
close FH;
Cheers,
KM
| Answer: How do I find the total number of lines in a file? contributed by fundflow You can use the $. variable ($/ defaults to \n)
while(<>){};
print "$. lines\n";
| Answer: How do I find the total number of lines in a file? contributed by Juerd This is a FAQ, answered in perlfaq5.
I quote from that document:
How do I count the number of lines in a file?
One fairly efficient way is to count newlines in the file.
The following program uses a feature of tr///, as documented in perlop. If your text file doesn't end with a
newline, then it's not really a proper text file, so this
may report one fewer line than you expect.
$lines = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;
This assumes no funny games with newline translations.
Read the FAQs, memorize the questions, so you know where to look when you need the answer.
perldoc Is a handy tool. perldoc -q count or perldoc -q lines would have given you the answer you wanted. | Answer: How do I find the total number of lines in a file? contributed by jospan In an AWK-ish kind of way, straight from the command prompt:
perl -lne "END { print $. }" myFilename | Answer: How do I find the total number of lines in a file? contributed by Nooks Try:
my $total = @{[<FILE>]};
print $total,"\n";
| Answer: How do I find the total number of lines in a file? contributed by newbie00 Try:
open(FILE, "filename");
@lines = <FILE>;
close(FILE);
$num = @lines;
--newbie00 | Answer: How do I find the total number of lines in a file? contributed by jospan In an AWK-ish kind of way, straight from the command prompt: perl -lne "END { print $. }" myFilename | Answer: How do I find the total number of lines in a file? contributed by JavaFan My favourite method to get the number of lines in a file is:
$line_count = `wc -l < $file`;
The redirection there is on purpose, without the < there, you get a different answer. |
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
Log In?
|
|
Chatterbox?
|
How do I use this? | Other CB clients
|
Other Users?
|
Others browsing the Monastery: (2) As of 2021-03-01 05:14 GMT
|
Sections?
|
|
Information?
|
|
Find Nodes?
|
|
Leftovers?
|
|
Voting Booth?
|
No recent polls found
|
Notices?
|
|
|