Re: Writing a modified .txt file issue [message #85373 is a reply to message #85370] |
Mon, 29 July 2013 13:39   |
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
|
|
|