There's an entry about counting lines in perlfaq5, which you have surely read prior to asking.
I guess you ask because you don't want to copy&paste the code, which is a laudable attitude in terms of code reuse and maintainability.
Let me tell you that I've written File::CountLines that ships (nearly) the same code as the FAQ uses, and eases the maintenance load on the stressed programmer.
| [reply] |
I have tried wc -l. but didnt work.
Define "did not work". What did you try? What did you get? What did you expect? Did you get an error? If so, what was it? What did you try to fix the error?
Is there any way to get no of lines ?
Yes, but all of them require you to open the file, read in all the data, and somehow count the number of newlines. Which is what wc -l does as well. And if that "doesn't work", I don't have high hopes for any alternative method.
| [reply] [d/l] |
Thanx all for sharing your knowledge.
Suggestion given by CountZero worked. I did same thing. I copied all whole file content into one array and used that array to get no of lines and content of file.
I have still one query. When I used suggestion given by moritz my $count = 0;
while( <$fh> ) { $count++; }
I was unable to get content of file. I think the reason for this was that you cant reuse file handle.
Can anyone tell me why this is so ?
@JavaFan ,
I tried to use this
$content = ` wc -l $Filehandle`
but I could not get output in $count as it should be. This worked fine on command prompt bt not on browser.
Regards,
Prafull
| [reply] |
When I used suggestion given by moritz my $count = 0; while( <$fh> ) { $count++; } I was unable to get content of file. I think the reason for this was that you cant reuse file handle. Can anyone tell me why this is so?
You've read in the entire file. The pointer is at the end of the file. Use seek to rewind. Or, if it's your intention to suck in the entire file anyway, do it on the first pass.
I tried to use this $content = ` wc -l $Filehandle` but I could not get output in $count as it should be.
Out of Define "did not work". What did you try? What did you get? What did you expect? Did you get an error? If so, what was it? What did you try to fix the error? you only answered the first question. Partially. $Filehandle sounds very fishy - is that a file handle, or does the variable $Filehandle actually contain the name of the file?
This worked fine on command prompt bt not on browser.
I give you one more chance: what did happen? Did you get an error? If so, which error? Check your error log as well. Or do you have some miracle wc which says, "Hmmm, I'm called by code written by Prafull. When he's running interactively I'm going to behave. But when it's run from a webserver, I'm going to return a random number!"?
| [reply] [d/l] [select] |
(Assuming \n is the EOL character).
use strict;
use warnings;
use 5.010;
open my $fh, '<', 'path/to/my/file.csv' or die "Could not open file: $
+!";
my @whole_file = <$fh>;
say 'Number of lines:', scalar @whole_file;
You can then of course use the data in the array to feed to methods of Text::CSV::Encoded so you do not have to read the file again. Of course if the file is really huge, you may run into memory problems.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |