Don't ask to ask, just ask | |
PerlMonks |
Re: Best way to read line x from a fileby davido (Cardinal) |
on Mar 29, 2004 at 16:09 UTC ( [id://340653]=note: print w/replies, xml ) | Need Help?? |
my $line2 = (<FILE>)[9]; Your method evaluates <FILE> in list context, resulting in a file slurp. Then you index into only one line, and let the rest of the slurp fall into the bit-bucket. I agree with Corion that Tie::File is a great solution. But I couldn't leave well enough alone, and had to come up with yet another way to do it. This solution still reads through the file up until it gets to the desired line. There's no way around that unless your lines are fixed-length.:
I hadn't seen anyone using $. yet. See perlvar. Update:Added last; to the loop. Thanks for the reminder.
Dave
In Section
Seekers of Perl Wisdom
|
|