comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Saving for loop values into arrays without know the final size of array
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Saving for loop values into arrays without know the final size of array [message #89154] Tue, 29 July 2014 12:24 Go to next message
sylvestergreene51 is currently offline  sylvestergreene51
Messages: 3
Registered: July 2014
Junior Member
I am dealing with a large data set and a for-loop with some if statements involved. thus I do not know what the final size of the for-loop will be and cannot declare the array dimensions beforehand which allows you to save your for-loop answers into an array. Does anyone know of a suggestion about how to work around this?
Re: Saving for loop values into arrays without know the final size of array [message #89155 is a reply to message #89154] Tue, 29 July 2014 12:35 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 7/29/14, 1:24 PM, sylvestergreene51@gmail.com wrote:
> I am dealing with a large data set and a for-loop with some if
> statements involved. thus I do not know what the final size of the
> for-loop will be and cannot declare the array dimensions beforehand
> which allows you to save your for-loop answers into an array. Does
> anyone know of a suggestion about how to work around this?
>

Easiest solution is to use the list class. There are other solutions if
you have an older version of IDL.

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: Saving for loop values into arrays without know the final size of array [message #89156 is a reply to message #89154] Tue, 29 July 2014 12:58 Go to previous messageGo to next message
Russell Ryan is currently offline  Russell Ryan
Messages: 122
Registered: May 2012
Senior Member
On Tuesday, July 29, 2014 3:24:15 PM UTC-4, sylveste...@gmail.com wrote:
> I am dealing with a large data set and a for-loop with some if statements involved. thus I do not know what the final size of the for-loop will be and cannot declare the array dimensions beforehand which allows you to save your for-loop answers into an array. Does anyone know of a suggestion about how to work around this?

Mike is right, if you have list, you should use list. However, this is will *NOT* work on IDL <7 and might be a problem for you (say if you distribute the code). If you want to use the "old school" way, then it's something like this.

You'll need to swap the for-loop for a while-loop and grow your arrays by concatenation. Suppose your array is called "arr" and you want it to be a floating point, then your code will look something like this:

arr=[0.0]
i=0L ;init the counter
okay =1b ;start the loop
while okay do begin
if i eq 0 then begin
arr(0)=FIRST_ELEMENT_OF_ARRAY
endif else begin
arr=[arr,ALL_OTHER_ELEMENTS_OF_ARRAY]
endelse
okay = SOME_TESTING_LOGIC_THAT_ONLY_YOU_KNOW
endwhile


This will work. You'll need to fill in the values of the array and stop condition, but we'd need to know more about your program to help. This is the simplest case and will work, however this can be made to be better in a great number of ways. First thing to do is change the line:

arr=[arr,XXXXX]

to something like

arr=[temporary(arr),XXXX]

this will save you memory and execute a tick faster (or always seems to).

Also, this is concatenation, which is horrifically slow in IDL, especially as the arrays get large. So, you're better off to concatenate as few times as possible. Instead of going through the relevant stuff, I'll point you to The Masters:

http://www.idlcoyote.com/code_tips/nullarray.html

Here, David Fanning details a discussion from JD Smith.

good luck...


Russell
Re: Saving for loop values into arrays without know the final size of array [message #89158 is a reply to message #89156] Tue, 29 July 2014 13:56 Go to previous messageGo to next message
gpeterso%ucsc.edu is currently offline  gpeterso%ucsc.edu
Messages: 2
Registered: July 2014
Junior Member
Thanks for the suggestions! I am currently using version 6.2
Re: Saving for loop values into arrays without know the final size of array [message #89159 is a reply to message #89158] Tue, 29 July 2014 14:31 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 7/29/14, 2:56 PM, gpeterso%ucsc.edu@gtempaccount.com wrote:
> Thanks for the suggestions! I am currently using version 6.2
>

I have an implementation of an "array list" (MGcoArrayList class) which
preallocates a fixed size array, then doubles the size every time you
over run it's current size. It handles remembering how long it is and
supports toArray, deletion, count, etc. It's in the collection/
directory of mglib, which you can find at:

github.com/mgalloy/mglib

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: Saving for loop values into arrays without know the final size of array [message #89160 is a reply to message #89159] Tue, 29 July 2014 17:17 Go to previous messageGo to next message
sylvestergreene51 is currently offline  sylvestergreene51
Messages: 3
Registered: July 2014
Junior Member
Thanks! Is the full name if the program mgcoarraylist_ut_define.pro? And I just use it the same way as the list() function?
Re: Saving for loop values into arrays without know the final size of array [message #89161 is a reply to message #89160] Tue, 29 July 2014 22:44 Go to previous message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
<sylvestergreene51@gmail.com> wrote:
> Thanks! Is the full name if the program mgcoarraylist_ut_define.pro?
> And I just use it the same way as the list() function?

That's it's unit test; mgcoarraylist__define.pro is in src/collection/.

-Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Automatically generating IDL bindings
Next Topic: [ANN] MIDLE - Almost an Alternative to EXECUTE

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:14:45 PDT 2025

Total time taken to generate the page: 0.00704 seconds