Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: reading the file using the line number

by davido (Cardinal)
on Nov 29, 2006 at 07:43 UTC ( [id://586635]=note: print w/replies, xml ) Need Help??


in reply to reading the file using the line number

Keep in mind that because "lines" are of variable length, there is no way to simply jump to line 1000 without first sifting through the file line by line to determine where line 1000 begins in the file. So any solution you come up with (short of maintaining an index or requiring uniform-length lines) will require that the file be read at least once, at least until the proper line number is finally found.

But you don't have to slurp the whole file into an array. It's fine to just read line by line, and take action when the desired line number is found.


Dave

  • Comment on Re: reading the file using the line number

Replies are listed 'Best First'.
Re^2: reading the file using the line number
by sgt (Deacon) on Dec 01, 2006 at 23:23 UTC

    actually this problem makes me think of a fairly recent discussion on p5p about the non-linear behaviour of the perl internal unicode implementation in the regex engine

    the problem is that to know the line position or the character position (supposing characters don't have all the same size) you have to count from the start...OR...do a bit of preparation, you put "markers" along the way

    maybe Tie::File uses something similar but essentially you keep the byte position of the start of every line = 0 modulo k, so to find line n *k +q seek to marker n and read q lines from there

    markers can be created as you fetch the lines, o pre-created in one go -- in the case of the regex engine, the algorithm could go both ways depending on the size of the "text" to match, or the maximum backtrack so far

    hth --stephan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found