IDL: Accessing strings using field names stored in variables?

If I have a structure with the field name 'fieldname', is it possible to access the data in this field using only a variable?

t

x = 'fieldname'

can do

data = struct. (x) somehow? I want to use the string in x as the name of the field.

+5
source share
1 answer

Yes, this is possible using the function TAG_NAMES:

tnames=TAG_NAMES(struct)
tindex=WHERE(STRCMP(tnames,'fieldname') EQ 1)
data=struct.(tindex)

TAG_NAMES , , struct. WHERE tnames , 'fieldname'. , struct.(tindex), .

, , tindex -, IDL -1.

+7

All Articles