Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDL: Accessing a structure tag with a variable name

In a previous function, I create and return a hash. Upon doing this, it returns the hash as a structure, and I use this as an input to this following function.

myStruct's tags are each a structure, each with a name and dataType tag.

I'm trying to iterate through each tag to find at which 'name' a certain dataType occurs.

pro plotter, myStruct

    numtags = n_tags(myStruct) 
    names = tag_names(myStruct)
    for varID = 0, numtags do begin
       if ( strcmp( myStruct.(names[varID]).datatype, 'Temperature, Head 1')) then print, varID

    endfor

 end

I get the following error after trying to run this: "Type conversion error: Unable to convert given STRING to Long."

What is causing this error? Can I access the tag using a variable name?

like image 969
Lauren R. Avatar asked Mar 13 '23 09:03

Lauren R.


1 Answers

You can do this, but not quite how you are. I think this is the problem:

myStruct.(names[varID])

since names[varID] is a string.

I'm assuming myStruct looks something like this:

myStruct = { tag1: {data:0L, datatype:'Some type'}, tag2: {data:1L, datatype:'Temperature, Head 1'}}

In general, you can access structures via the tag name or the index. So,

myStruct.(0)
myStruct.tag1

will both give you the first value in the first tag of the structure (and you can increment the index as needed for other tags). In this case, these will yield the structure "stored" in tag1.

If so, then this should work:

pro plotter, myStruct

numtags = n_tags(myStruct) 
names = tag_names(myStruct)
for varID = 0, numtags-1 do begin
   if ( strcmp( myStruct.(names[varID]).datatype, 'Temperature, Head 1')) then print, names[varID]
endfor

end
like image 171
Phillip Bitzer Avatar answered May 16 '23 07:05

Phillip Bitzer