Re: Merits of different ways of 'extending' arrays [message #85862 is a reply to message #85805] |
Sat, 14 September 2013 18:11   |
Andy Sayer
Messages: 127 Registered: February 2009
|
Senior Member |
|
|
Thanks for the continuing tips!
The first suggestion (allocate a 'big enough' array up-front, rather than continually extend) worked great for my purposes, so that's what I stuck with, given that it was also very simple. Although I appreciate the continued suggestions.
Andy
On Tuesday, September 10, 2013 4:08:40 PM UTC-4, suicida...@gmail.com wrote:
> 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.
|
|
|