Use Ascii_Template/Read_Ascii results to write a file [message #87505] |
Sun, 09 February 2014 20:56 |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
Say I have a template and structure returned by Ascii_Template() and Read_Ascii(), respectively. In general, I do not know how many fields the structure has. I now want to turn around and write the data to a file. Is there a way to do this without a double loop?
pro test_write_ascii
compile_opt strictarr
;Information available from Ascii_Template and Read_Ascii
str_fmt = ['i', 'a']
nLines = 4
nFields = 2
;Structure returned by Read_Ascii()
struct = {field0: [0,1,2,3], $
field1: ['a', 'b', 'c', 'd']}
;Open a file
openw, lun, '/test.txt', /get_lun
;Loop through each data point
for i = 0, nLines - 1 do begin
;Make a comma-delimited string
str_field = ''
for j = 0, nFields - 1 do str_field += String( struct.(j)[i], FORMAT='(' + str_fmt[j] + ')' ) + ','
;Print the string to the file
printf, lun, str_field
endfor
free_lun, lun
end
I also tried
myList = List([0,1,2,3], ['a', 'b', 'c', 'd'])
Print, myList
but the list prints horizontally, not vertically, which is what I am after.
|
|
|