On Aug 15, 11:38 am, [EMAIL PROTECTED] (Toddy Prawiraharjo) wrote:
> Is it possible to read only part of file in perl?

Yes.  You can stop reading at any time.

> At this moment I have script similar to:
>
> open (FH, "file");
> @FH = <FH>;

Well there's your problem.  You just read the entire file, all at
once.

> foreach $line(@FH){

And now you're just looping through an array of strings.  There is no
longer a concept of "file".

>         processing linebyline}

No.  You're not processing line by line.  You already read ALL lines
into one giant array.

> close FH;
>
> However, I only need certain part of file only to be read (let say beginning
> 20 lines).

So do that.  Only read 20 lines.  Don't read the whole file.

open my $fh, '<', "file" or die $!;
while (my $line = <$fh>) {
   process_line($line);
   last if $. == 20;
}

> Above method will be taking lot of time to read file up to 5mb in
> size (.ps file), so how do I this in perl? Any module or other slick way?
>
> So far my solution is using system call using unix's head -n 20.

Find whoever or whatever told you to process a file by reading all
lines into an array, and smack them around a little.  Horrible
programming habbit to get into.

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to