Re: Random Access Text (like getline) [message #39815] |
Tue, 22 June 2004 16:58  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
Michael Lefsky wrote:
> I am looking for way to randomly access a text file, as getline will
> do in python. Essentially, getline will allow you to access any line
> in a text file by giving it the line number in the file (e.g.
> getline(100)). I can't see a way to do that with a text file in
> IDL....
There are many routines around that will read a text file into a string
array, whence you can access any line. The one I know about is
MGH_TXT_RESTORE in the Motley library:
http://www.dfanning.com/hadfield/idl/README.html
http://www.dfanning.com/hadfield/idl56/README.html
This routine boasts 4 (!) different methods of accumulating the strings
before assembling them into an array. Which is excessive, obviously, but
I was curious about the relative performance.
If you just want, say, the 100th line of a 1000000-line file, it would
be wasteful to read the whole file. I believe David's code in another
post reads up to the required line, and not beyond.
I looked up getline on the Python documentation search site and found...
getline() (in module linecache)
The linecache module allows one to get any line from any file, while
attempting to optimize internally, using a cache, the common case where
many lines are read from a single file. This is used by the traceback
module to retrieve source lines for inclusion in the formatted traceback.
...Very cool! I doubt anyone's implemented anything like this in IDL.
Perhaps you would like to give it a try and post the code when you've
finished.
--
Mark Hadfield "Ka puwaha te tai nei, Hoea tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|