Re: Empty arrays? [message #63437] |
Mon, 10 November 2008 14:56  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On Nov 10, 3:35 pm, Spon <christoph.b...@gmail.com> wrote:
> For these sorts of cases, I tend to use empty pointers. Here's an
> example using pointers to provide the functionality I think you're
> looking for:
>
> var = ptr_new(/allocate_heap)
> help, *var
> IDL> <PtrHeapVar1> UNDEFINED = <Undefined>
>
> This article by JD Smith on David Fanning's website should help get
> you started on pointers:http://www.dfanning.com/misc_tips/pointers.html
Yes, empty pointers work well for these type of things, but there are
still two issues with them in this situation:
1. you still have to check to see if *var is undefined to determine
if you are appending to or creating an array.
2. repeatedly appending to an array is inefficient
A better solution is to create a reasonable sized array to begin with.
Then fill in values and keep track of how many are filled in. If the
array fills up, create a new bigger one and copy the values over. Of
course, this is even more of a hassle with bookkeeping, so I made an
object that does this for me, see:
http://michaelgalloy.com/2006/04/24/collection-package-mgarr aylist.html
Of course, it would be nicer if IDL just allowed empty arrays, but
there would be a lot of backward compatibilities if that were the
case.
Mike
--
www.michaelgalloy.com
Tech-X Corporation
Associate Research Scientist
|
|
|