PDA

View Full Version : help with reading a file


z1bob
07-09-2004, 06:39 PM
Any suggestions for optimizing this code:

open(INFILE,$file);
for ($int=0; $int < $Nth; $int++){
$line=<INFILE>;
}
close(INFILE);

I would like to use the seek() function to get the $Nth line of $file
but seek(INFILE, $Nth, 0) goes to the $Nth character.
The lines of $file are not uniform length.

MattJakel
07-10-2004, 06:33 AM
If you just want to get to the $Nth line for reading, you can write the entire file to an array and use @array[$Nth-1]. If you need to insert something there or something, the only way I know to do it would be to use a for loop saving to a variable every time or something to get it to go to the next line.

Matt

Afrow UK
07-10-2004, 01:06 PM
It would be easiest to do:

open (FILE, "<file.ext");
my @lines = <FILE>;
close (FILE);

my $line2 = $lines[1];
my $line4 = $lines[3];

etc
Remember accessing array's are zero-based.

-Stu