RE: Count the number of lines in a file without actually iteratin g through the file

From: Rob Hanson (rhanson_at_aptegrity.com)
Date: 02/26/04


To: 'Jason Normandin' <jason_normandin@charter.net>, Perl Beginners	 <beginners@perl.org>
Date: Wed, 25 Feb 2004 19:25:53 -0500


> Is there a way to determine the number of lines
> in a file without actually iterating through the
> file and incrementing a file?

No.

> I found the following on perlmonks.org, it works
> great but this is command line syntax

When you deparse that command (see below), you can see that all it does is
read each line of the file and set $_ to the line number (*.). After
reading in each line it then prints the result. So there is no silver
bullet.

$ perl -MO=Deparse -lpe '}{*_=*.}{' file

LINE: while (defined($_ = <ARGV>)) {
    chomp $_;
}
{
    *_ = *.;
}
{
    ();
}
continue {
    die "-p destination: $!\n" unless print $_;
}

-----Original Message-----
From: Jason Normandin [mailto:jason_normandin@charter.net]
Sent: Wednesday, February 25, 2004 6:56 PM
To: Perl Beginners
Subject: Count the number of lines in a file without actually iterating
through the file

Hi List.

 

Is there a way to determine the number of lines in a file without actually
iterating through the file and incrementing a file?

 

I found the following on perlmonks.org, it works great but this is command
line syntax

:

perl -lpe '}{*_=*.}{' file

 

How could I integrate this into a script without invoking another instance
of Perl?

 

Thanks !

~Jason

------------------------------------

 

 "If, after the first twenty minutes, you don't know who the sucker at the
table is, it's you."

 

------------------------------------

 



Relevant Pages

  • Re: Python question
    ... Harry wrote: ... What command do I use to get the value corresponding to 'min'? ... Iterating would be the best way. ...
    (comp.lang.python)
  • Re: Proposal: new module, Array::Each?
    ... this should allow iterating over the same set of ... > arrays using different iterators. ... use warnings; ... some type of syntax as shown below would work..... ...
    (comp.lang.perl.misc)
  • Count the number of lines in a file without actually iterating through the file
    ... iterating through the file and incrementing a file? ... "If, after the first twenty minutes, you don't know who the sucker at the ...
    (perl.beginners)
  • Re: Error coding question
    ... > How does the entire string fill without iterating the Mid command? ... The Mid statement would appear to function like the old MVC assembly command ... You could think of it as keeping two pointers one of them for the ...
    (microsoft.public.vb.general.discussion)
  • Re: Using for each, in with STL containers with non-const iterators
    ... modify the elements of the container while iterating. ... I was expecting this to work: ... This 'for each' syntax doesn't exist in C++. ...
    (microsoft.public.vc.stl)