comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Random Access Text (like getline)
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Random Access Text (like getline) [message #39815] Tue, 22 June 2004 16:58
Mark Hadfield is currently offline  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)
Re: Random Access Text (like getline) [message #39821 is a reply to message #39815] Tue, 22 June 2004 15:48 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Michael Lefsky writes:

> 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....

Here is something quick and dirty, hacked from something else.
Seems to work with the light testing I've given it on a couple
of text files. Might get you started in the right direction,
anyway. :-)

Cheers,

David

;*********************************************************** *
FUNCTION GetLine, lineNumber, filename

; This utility routine is used to return a specific line in a text file.

IF N_Elements(maxrows) EQ 0 THEN maxrows = 500L
IF N_Elements(lineNumber) EQ 0 THEN lineNumber = 0
IF N_Elements(filename) EQ 0 THEN BEGIN
filename = Dialog_Pickfile(Title='Find Line in this File...')
IF filename EQ "" THEN RETURN, ""
ENDIF

OpenR, lun, filename, /Get_Lun

Catch, error
IF error NE 0 THEN BEGIN
count = count-1
Free_Lun, lun
ok = Dialog_Message('Selected text file has only ' + $
StrTrim(count,2) + ' lines in the file.')
RETURN, ""
ENDIF

RESTART:

count = 0L
line = ''
REPEAT BEGIN
ReadF, lun, line
count = count + 1

; Try again if you hit MAXROWS without encountering the
; end of the file. Double the MAXROWS parameter.

IF count EQ maxrows THEN BEGIN
maxrows = maxrows * 2
Point_Lun, lun, 0
GOTO, RESTART
ENDIF

ENDREP UNTIL count GT lineNumber

Free_Lun, lun
RETURN, line
END
;*********************************************************** *

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Interpolate contours
Next Topic: Re: Displaying a grid

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 17:24:57 PDT 2025

Total time taken to generate the page: 0.00467 seconds