[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Only read the first 30 lines of a file?



At 20.54 -0400 1999.04.22, Chilton Webb wrote:
>In MacPerl, what's a good way to read only the first 30 lines of a file?
>I have a program that looks for certain info in several thousand files,
>and these files are hundreds of k long. The info, however, is typically
>in the first 10 or 20 lines. How can I do this?

open FILE, "somefile" or die $!;
while (<FILE>) {
  $data .= $_;
  last if $. >= 30;
}

$. is the line number variable.  You could also keep your own counter:

while (<FILE>) {
  $data .= $_;
  last if ++$counter >= 30;
}

You could also do push(@data, $_) if you want the data in an array.  Or
another kind of loop:

for (0..30) {
  $data .= <FILE>;
}

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org