Re: Merits of different ways of 'extending' arrays [message #85805 is a reply to message #85798] |
Tue, 10 September 2013 13:08   |
suicidaleggroll
Messages: 14 Registered: September 2013
|
Junior Member |
|
|
On Monday, September 9, 2013 4:41:10 PM UTC-6, suicida...@gmail.com wrote:
> 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
Wish I could edit my post...
There should be an "idx += num" next to the ptr_free at the end of the second loop.
|
|
|