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

Re: Concernant:Re: [MacPerl] Counting the number of lines in a



jose.stephane@uqam.ca wrote:

>Is it possible to read a particular line number from a file?
   [ ... ]
>How can I implement something like '$myline = line 8 of MYFILE' in proper 
>perl?

If all the lines in the file are the same length, then you can figure out 
where in the file your line begins, and use seek() to get there. (For 
instance, if every line is 81 bytes long (including the line separator) 
then you need to seek 567 bytes into the file to get to line 8.)

If not, you have to read through the file looking for line separators. As 
Jim Flanagan pointed out, the $. special variable makes this simple, e.g.:

    open MYFILE, "myfile" or die "myfile: $!";
    do { $myline = <MYFILE> } while ( $. < 8 );

Cheers,
Andrew.


--
Andrew McRae <mcrae@internet.com>