Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How do I seek to a certain position in a file?

by simonflk (Pilgrim)
on Nov 13, 2002 at 15:57 UTC ( [id://212613]=note: print w/replies, xml ) Need Help??


in reply to How do I seek to a certain position in a file?

GhodMode said:
my @array = <FILE>; close FILE; # Print line 10 print $array[10];

Not quite. That prints line 11. I would do something like this:

my $findline = 10; while (<FILE>) { next unless $. == $findline; # .. do something last; #don't read any more }

For the original question "do something" would probably be a seek backwards to the desired char, or perhaps more sensibly, stop the line before and seek forwards.

for more info on $. and friends, see perldoc perlvar

-- simonflk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://212613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found