Re: Merits of different ways of 'extending' arrays [message #85798 is a reply to message #85726] |
Mon, 09 September 2013 15:41   |
suicidaleggroll
Messages: 14 Registered: September 2013
|
Junior Member |
|
|
Another option is to set up a pointer array nfiles long before the loop, inside the loop load the file and find the valid points, then put that array into that file's pointer, while incrementing a counter to keep track of the total number of points. When you're done, you have all of your data saved in pointers (one per file), and a count of the total number of valid points. Then you allocate your array, loop back through the elements of the pointer array, and fill the array as necessary. Something like:
f = file_search(path, count=nfiles)
ptrs = ptrarr(nfiles)
num = 0l
for i=0l,nfiles-1 do begin
;; load contents of file
is_valid = where(stuff, n_valid)
if n_valid gt 0 then begin
num += n_valid
ptrs[i] = ptr_new(f.var_1[is_valid])
endif
endfor
data = fltarr(num)
idx = 0l
for i=0l,nfiles-1 do begin
if ptr_valid(ptrs[i]) then begin
num = n_elements(*ptrs[i])
data[idx:idx+num-1] = *ptrs[i]
ptr_free, ptrs[i]
endif
endfor
|
|
|