Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

File length ??

by Anonymous Monk
on Sep 12, 2002 at 18:28 UTC ( [id://197302]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Is there an easier way to determine the length of a data file. The file I'm reading in has blank lines, that I DON'T want in my array.

This is my current code:

while(<DATA>){ # Populate the data array if ($_ ne ""){ push @data, $_; } } close DATA; my $length = @data; print "LENGTH: $length\n";
My check for NULL doesn't seem to work....
Any suggestions?

Replies are listed 'Best First'.
Re: File length ??
by Aristotle (Chancellor) on Sep 12, 2002 at 18:36 UTC
    I don't because there's still an end of line marker. You could check for that explicitly:
    while(<DATA>){ next if $_ eq $/; push @data, $_; } # shorter: $_ ne $/ and push @data, $_ while <DATA>;
    See perldoc perlvar about $/. Or you could remove the linebreaks:
    while(<DATA>){ chomp; next if $_ eq ''; push @data, $_; }
    See perldoc -f chomp.

    Makeshifts last the longest.

Re: File length ??
by Moonie (Friar) on Sep 12, 2002 at 18:36 UTC
Re: File length ??
by dws (Chancellor) on Sep 12, 2002 at 18:50 UTC
    Please take a moment before you post to make sure you're asking the question you indend to have answered. "File size" and "number of non-blank lines in a file" are two very different things.

    The quick way to determine a file size (if you're interested) is by using -s or stat(). Consult the docs.

Re: File length ??
by charnos (Friar) on Sep 12, 2002 at 20:00 UTC
    Are you trying to determine the number of lines in the data file after taking out blank lines? Also, what's blank? Does blank include spaces? My suggestion would be as such:
    @data = grep { $_ !~ /^\s*$/ } @a; my $length = scalar @data;
    That is assuming that blank lines may or may not include spaces. If you know that they will not, you could save yourself a good deal of time (especially for large files) by simply testing $_ ne "" in the grep.
Re: File length ??
by thor (Priest) on Sep 13, 2002 at 00:06 UTC
    perl does a wonderful job of pattern matching. Here's what I've got:
    while(<DATA>){ # Populate the data array if ($_ !~ /^\s*$/){ push @data, $_; } }
    This gets rid of lines that have only whitespace. If you want truly blank lines take the \s* out of the regex.

    thor

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://197302]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found