*nods* its why i said careful with what kind of file you use... large files
dont respond well to this.

a very clean way to do it imho is something like this:


### open a file specified on the command line ###
open(I,"$ARGV[0]);

### undef the line seperator, gobble up the file in $in ###
{local $/; $in = <I>; }

###count the occurrences of X in $in. change X to whatever you want your
seperator to be, ie, \n ###
$count = ($in =~ t/X//);


this will give you the amount of occurrences of X (perlfaq 4) in your file
and when counting newlines, the amount of lines...

Regards,

Jos Boumans



----- Original Message -----
From: "Jeff Pinyan" <[EMAIL PROTECTED]>
To: "Jos I Boumans" <[EMAIL PROTECTED]>
Cc: "Susan Richter" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Saturday, May 05, 2001 5:05 PM
Subject: Re: Counting lines on a file


> On May 5, Jos I Boumans said:
>
> >open(I,"$ARGV[0]");
> >my @foo = <I>;
> >print scalar @foo;
>
> It is considered a dubious practice to read a file into an array; this can
> use of lots of memory.  It is particularly wasteful if the goal is just to
> find out how many lines there are.
>
> Another wasteful use is:
>
>   @lines = <FILE>;
>   for (@lines) { ... }
>
> when a simple:
>
>   while (<FILE>) { ... }
>
> will do.
>
> I have an article about files and filehandling on my web site:
>
>   http://www.pobox.com/~japhy/articles/pa/2000-01.html
>
> --
> Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
> Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
> Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
> Acacia Fraternity, Rensselaer Chapter.         Brother #734
>
>

Reply via email to