Re: null array [message #36471] |
Wed, 24 September 2003 18:35  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Tomson writes:
> But What I want to do is to initialize several arrays which are in common
> block and their length will be changed for different call and the lengths
> of them may be zero. If the lengthes of them are zero, the process will be
> changed. If there isnot any initialization, the array return to the main
> program may be the former ones.
If I understand you correctly, you may find a pointer
useful in this situation. A pointer could point to
a "null array", in the sense that it would point to
an "undefined variable":
ptr = Ptr_New(/Allocate_Heap)
IF N_Elements(*ptr) EQ 0 THEN Print, 'Pointer undefined' ELSE $
*ptr = [initValue]
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
|
|
|
|
|
|
|
|
Re: null array [message #36544 is a reply to message #36471] |
Tue, 30 September 2003 12:36  |
Stein Vidar Hagfors H[2]
Messages: 28 Registered: October 2002
|
Junior Member |
|
|
Well, David, always pluggin' the pointers, eh?
Using pointers simply because of their ability to point to undefined
variables seems a bit overkill to me, though. A variable in itself
is just as good at representing an undefined variable... as in:
if n_elements(variable) eq 0 then print,"Variable undefined" $
else variable = [initValue]
And the way to "undefine" a variable is the good old
dummy = temporary(variable)
David Fanning <david@dfanning.com> writes:
> Tomson writes:
>
>> But What I want to do is to initialize several arrays which are in common
>> block and their length will be changed for different call and the lengths
>> of them may be zero. If the lengthes of them are zero, the process will be
>> changed. If there isnot any initialization, the array return to the main
>> program may be the former ones.
>
> If I understand you correctly, you may find a pointer
> useful in this situation. A pointer could point to
> a "null array", in the sense that it would point to
> an "undefined variable":
>
> ptr = Ptr_New(/Allocate_Heap)
> IF N_Elements(*ptr) EQ 0 THEN Print, 'Pointer undefined' ELSE $
> *ptr = [initValue]
>
> Cheers,
>
> David
>
> --
> David W. Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Phone: 970-221-0438, E-mail: david@dfanning.com
> Coyote's Guide to IDL Programming: http://www.dfanning.com/
> Toll-Free IDL Book Orders: 1-888-461-0155
--
------------------------------------------------------------ --------------
Stein Vidar Hagfors Haugan
ESA SOHO SOC/European Space Agency Science Operations Coordinator for SOHO
NASA Goddard Space Flight Center, Tel.: 1-301-286-9028
Mail Code 682.3, Bld. 26, Room G-1, Cell: 1-240-354-6066
Greenbelt, Maryland 20771, USA. Fax: 1-301-286-0264
------------------------------------------------------------ --------------
|
|
|
Re: null array [message #36562 is a reply to message #36478] |
Fri, 26 September 2003 02:24  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
JD Smith writes:
> But this is also slow, since you reallocate the array on each iteration
> (which starts to hurt when the array gets large). Far better is to
> pre-allocate the array to roughly the correct size, and grow it as
> necessary. Search this newsgroup for "Is there a standard 'null' array"
> for a thread on this very topic.
Oh, oh. I've started to write this up twice now.
Good thing I'm consistent in my naming convention
or I would be doing every project two or three times.
Does this remind you of anything? :-(
Anyway, you can find a discussion about "null arrays"
here, taken from J.D.'s excellent post on the topic.
http://www.dfanning.com/code_tips/nullarray.html
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|