Re: Defining Integer Field in a Structure [message #27732 is a reply to message #27730] |
Thu, 01 November 2001 14:05   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
JD Smith (jdsmith@astro.cornell.edu) writes:
> Brian Bell wrote:
>>
>> Is there a way to define a field within a structure whose type is
>> integer? In my case, I want the field to be any integer from 0 to 4.
>> I know you can assign an actual integer to the field, but I don't want
>> to do this because I want the field value to change within certain
>> procedures and to return that value to the procedures calling it. I
>> would appreciate any help you might be able to offer.
>>
>
> This is illegal:
>
> IDL> a={0:"this", 1:"is",2:"a",3:"funny",4:"test"}
>
> so the short answer is no. However, if you just want 4 numbered
> "slots", try an array:
>
> IDL> a={vals:['this','is','a','funny','test']}
> IDL> print,a.vals[1]
> is
>
> if you'd like the types of each slot to be different, try at a pointer
> array. If you'd like the number of "slots" to be variable, try a
> pointer to a pointer array.
I have a feeling Brian was looking for something a LOT
simpler than this. :-)
I think he is confused between a specific *instance* of
a structure and the *definition* of a structure. Especially
because he has probably seen most structured both defined
and populated with the same statement. For example, like
this:
IDL> struct = {MYSTRUCT, a:3, b:5.4}
Here the variable struct is a specific *instance* of the
structure whose definition is defined in IDL as "MYSTUCT".
For example, you can create another structure like this:
IDL> struct2 = {MYSTRUCT}
IDL> Help, struct2, /Structure
** Structure MYSTRUCT, 2 tags, length=8, data length=6:
A INT 0
B FLOAT 0.000000
The fields in the second structure are defined *by* the
fields that appeared in the original structure definition
statement. These are the "null" field values, if you like.
You can assign any value you like to them:
IDL struct2.a = 4
If you wish to create a stucture definition without
actually creating an instance of a structure, you
put the structure definition in a file, like this:
PRO newstruct__define.pro
struct = {NEWSTRUCT, a:0, b:0.0, c:fltarr(10)}
END
Then you can use the structure definition whenever you
like:
IDL> var = {NEWSTRUCT}
IDL> var.a = 5
IDL> var.c = Findgen(10)*5
Cheers,
David
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting
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
|
|
|