in reply to Matching on a specific line of text
If it doesn't match, it fills in undef for the data.open IN, "<", $file or die "Can't open file $file: $!"; my %data; while(<IN>) { if($. == 1 || $. == 2) { my($number) = /([\d.]+)\s*$/; $data{$.} = $number; } }
It's a bit silly for such small line numbers, but for larger numbers this approach begins to make sense.
I use a hash as a sparse array, it keeps a connection between the line number and the read data without wasting space for inbetween line numbers. Again, this makes most sense if your line numbers are much bigger than 1 or 2.
(update fixed error in code, thanks to davidrw for pointing that out.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Matching on a specific line of text
by bart (Canon) on May 07, 2006 at 19:29 UTC | |
|
Re^2: Matching on a specific line of text
by davidrw (Prior) on May 07, 2006 at 18:54 UTC | |
by bart (Canon) on May 07, 2006 at 19:37 UTC |