Re: Evaluate a string as a variable name [message #70723] |
Tue, 04 May 2010 07:48  |
laurel
Messages: 3 Registered: June 2005
|
Junior Member |
|
|
Brilliant! Thanks so much.
-Laurel
On May 3, 4:24 pm, mgalloy <mgal...@gmail.com> wrote:
> On 5/3/10 4:02 PM, laurel wrote:
>
>
>
>> I have a structure that has multiple name/value pairs, and I want to
>> extract the value that corresponds with the name. However, I am only
>> passing in a string of the name, not the structure.name, so I need a
>> way to combine the two such that I am evaluating the string. It might
>> be easier to explain with an example:
>
>> ***
>> ;create a simple structure
>> struct={name1:9, $
>> name2:5, $
>> name3:1}
>
>> ;variable that matches the part of the structure I want
>> x='name2'
>
>> ;create a string that if it were a variable, it would be part of the
>> structure, 'struct.name2'
>> y='struct'+'.'+x
>
>> ;want to have z=5, or z=struct.name2 rather than z='struct.name2'
>> ;Here evaluate stands in for whatever I would do to make that happen
>> z=evaluate(y)
>> ***
>
>> Does anyone know how to make z equal to the value corresponding with
>> struct.name2?
>
> How about this:
>
> x = 'name2'
> struct = { name1:9, $
> name2:5, $
> name3:1 }
> names = tag_names(struct)
> ind = where(strupcase(x) eq names, count)
> if (count gt 0L) then z = struct.(ind[0])
>
> Mike
> --www.michaelgalloy.com
> Research Mathematician
> Tech-X Corporation
|
|
|
Re: Evaluate a string as a variable name [message #70729 is a reply to message #70723] |
Mon, 03 May 2010 15:24   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 5/3/10 4:02 PM, laurel wrote:
> I have a structure that has multiple name/value pairs, and I want to
> extract the value that corresponds with the name. However, I am only
> passing in a string of the name, not the structure.name, so I need a
> way to combine the two such that I am evaluating the string. It might
> be easier to explain with an example:
>
> ***
> ;create a simple structure
> struct={name1:9, $
> name2:5, $
> name3:1}
>
> ;variable that matches the part of the structure I want
> x='name2'
>
> ;create a string that if it were a variable, it would be part of the
> structure, 'struct.name2'
> y='struct'+'.'+x
>
> ;want to have z=5, or z=struct.name2 rather than z='struct.name2'
> ;Here evaluate stands in for whatever I would do to make that happen
> z=evaluate(y)
> ***
>
> Does anyone know how to make z equal to the value corresponding with
> struct.name2?
How about this:
x = 'name2'
struct = { name1:9, $
name2:5, $
name3:1 }
names = tag_names(struct)
ind = where(strupcase(x) eq names, count)
if (count gt 0L) then z = struct.(ind[0])
Mike
--
www.michaelgalloy.com
Research Mathematician
Tech-X Corporation
|
|
|
Re: Evaluate a string as a variable name [message #70809 is a reply to message #70723] |
Tue, 04 May 2010 19:31  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On May 4, 10:48 am, laurel <laur...@gmail.com> wrote:
> Brilliant! Thanks so much.
> -Laurel
The IDL Astronomy library also has the very handy routine TAG_EXIST(),
which allows you to query structure members by name.
You would do something like this.
if tag_exist(struct, x, index=ii) then begin
y = struct.(ii)
endif else begin
message, "ERROR: could not find '"+x+"' in structure"
endelse
TAG_EXIST() appears to be completely stand-alone, i.e. no dependencies
on other functions in the library.
http://idlastro.gsfc.nasa.gov/ftp/pro/structure/tag_exist.pr o
Craig
|
|
|