Dynamically create variable from header when reading in ascii data [message #75914] |
Mon, 09 May 2011 04:59  |
rjp23
Messages: 97 Registered: June 2010
|
Member |
|
|
I'm sat here defining a huge list of variables to be read in from an
ascii file and I'm sure there must be an easier way...
I have an ascii file with multiple columns of data, each column has
the column name on the first row. There are then thousands of rows of
data.
I started off with just a few columns but as things have progress I
now have a lot (hundreds) of columns and doing it this way is getting
frustrating.
This is what I'm doing:
header=strarr(1)
data=strarr(file_lines(summary_file)-n_elements(header))
openr, lun, summary_file, /get_lun
readf, lun, header
readf, lun, data
free_lun, lun
col_titles=strsplit(header, ' ', /extract)
ap_pres_01=fltarr(n_elements(data))
ap_pres_02=fltarr(n_elements(data))
ap_pres_03=fltarr(n_elements(data))
ap_pres_04=fltarr(n_elements(data))
ap_pres_05=fltarr(n_elements(data))
ap_pres_06=fltarr(n_elements(data))
etc etc
for i=0L,n_elements(data)-1 do begin
ap_pres_01[i]=(strsplit(data[i], ' ', /extract))
[where(strmatch(col_titles, 'ap_pres_01'))]
ap_pres_02[i]=(strsplit(data[i], ' ', /extract))
[where(strmatch(col_titles, 'ap_pres_02'))]
ap_pres_03[i]=(strsplit(data[i], ' ', /extract))
[where(strmatch(col_titles, 'ap_pres_03'))]
ap_pres_04[i]=(strsplit(data[i], ' ', /extract))
[where(strmatch(col_titles, 'ap_pres_04'))]
ap_pres_05[i]=(strsplit(data[i], ' ', /extract))
[where(strmatch(col_titles, 'ap_pres_05'))]
ap_pres_06[i]=(strsplit(data[i], ' ', /extract))
[where(strmatch(col_titles, 'ap_pres_06'))]
etc etc
endfor
What I'd like to do is dynamically create the variables from the
col_titles variable and then populate them. I wrote something that did
this using the execute command to create the variables but it was a
bit messy and I believe I'm right in saying that execute won't work in
the virtual machine (not that I necessarily need it to but possibly
might in the future so I'm reluctant to build the whole code around it
if there's a nicer alternative).
Thanks in advance
|
|
|