Array of Variable Element Arrays
QUESTION: Is there any way to handle nested arrays in IDL (that is, an array of arrays, or lists)? Say I want to populate an array with sub-arrays of variable length, something like this:
[ [1,2,3,4]
[1,2,3]
[1,2,3,4,5,6,7,8,9]
[1]
['']
[1,2] ]
If I knew the maximum number of arrays, and the maximum dimension for all of those arrays, I could make a 2-D array (rectangular) - but, a) I don't know this beforehand and, b) even if I did, it would be a huge waste of space and efficiency. I've perused a lot of web-postings, but have not been able to find an answer, save using a structure of multiple arrays - blech!
![]()
ANSWER: Typically, we use a pointer array for this purpose:
IDL> variable = $
[ Ptr_New([1,2,3,4], /No_Copy), $
Ptr_New([1,2,3], /No_Copy), $
Ptr_New([1,2,3,4,5,6,7,8,9], /No_Copy), $
Ptr_New([1], /No_Copy), $
Ptr_New(/Allocate_Heap), $
Ptr_New([1,2], /No_Copy) ]
IDL> Help, variable
VARIABLE POINTER = Array[6]
IDL> Print, *variable[2]
1 2 3 4 5 6 7 8 9
IDL> Print, *variable[1]
1 2 3
Note that the fifth pointer in this array is a valid pointer, even though it doesn't yet contain any data.
IDL> Print, Ptr_Valid(*variable[4])
1
I would get an error if I use this array in an expression, because the pointer points to an undefined variable:
IDL> Print, *variable[4] * 3
Variable is undefined: .
Execution halted at: $MAIN$
But, because it is a valid pointer, I can fill it with data at any time:
IDL> *variable[4] = Findgen(6)
IDL> Print, *variable[4]
0.000000 1.00000 2.00000 3.00000 4.00000 5.00000
Also note that I can store any kind of information I like in the pointer array:
IDL> *variable[2] = 'This is a string stored in the pointer array'
IDL> *variable[3] = {field1: 'This is a structure', field2:[2,3]}
IDL> Help, *variable[2]
<PtrHeapVar14013>
STRING = 'This is a string stored in the pointer array'
IDL> Help, *variable[3]
** Structure <2b809d0>, 2 tags, length=16, data length=16, refs=1:
FIELD1 STRING 'This is a structure'
FIELD2 INT Array[2]
If pointers are unfamiliar, you can learn more by reading the Pointer Tutorial.
![]()
Copyright © 2006 David W. Fanning
Last Updated 9 October 2006