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

Home » Public Forums » archive » Writing a modified .txt file issue
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Writing a modified .txt file issue [message #85373 is a reply to message #85370] Mon, 29 July 2013 13:39 Go to previous messageGo to previous message
Heinz Stege is currently offline  Heinz Stege
Messages: 189
Registered: January 2003
Senior Member
On Mon, 29 Jul 2013 13:03:02 -0700 (PDT), Phillip Bitzer wrote:

> I also have an ASCII data file that contains both numeric and string columns. I'm not a fan. What I'm doing now is read the whole thing in as a string array:
>
> nLines = FILE_LINES(filename)
> OPENR, lun, filename, /GET_LUN
> word = STRARR(nLines)
> READF, lun, word
> FREE_LUN, lun
>
> Then, I loop through each line, using STR_SPLIT to divvy up the fields at the space that separates the columns, and throw them into the output structure (not defined here for simplicity)
>
> FOR i=0L, nLines-1 DO BEGIN
> this_row = STRSPLIT(word[i], ' ', /extract)
> data[i].date = this_row[0]
> data[i].time = this_row[1]
> data[i].latitude = DOUBLE(this_row[2])
> data[i].longitude = DOUBLE(this_row[3])
> ....
> ENDFOR
>
> I have been looking for a better/more efficient way of doing this. Can't seem to find a good way, but maybe Coyote has cooked up something :-) ReadCol works about as quickly as my code.
>
This type of files typically have much more lines than columns.
Therefore it is more efficient to run the loop over a few columns than
over all the lines. The following code more deals with arrays instead
of scalars:

for i=0,nColumns-2 do begin
pos=strpos(word,' ')
data.(i)=strmid(word,0,transpose(pos))
word=strtrim(strmid(word,transpose(pos)),1)
end
data.(nColumns-1)=word

Try it.

Cheers, Heinz
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: Find all user-defined structure definitions
Next Topic: Multiple plots with plot function

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

Current Time: Sat Oct 11 02:31:10 PDT 2025

Total time taken to generate the page: 0.10746 seconds