Re: [Q] extracting value from a structure ... [message #12781] |
Thu, 10 September 1998 00:00 |
Thomas A. McGlynn
Messages: 23 Registered: March 1996
|
Junior Member |
|
|
dEdmundson@Bigfoot.com wrote:
>
> Consider the following:
>
> r = {name: 'john', age=18}
> s = 'age'
>
> Given the string s, is there an easy way to
> extract the value of the corresponding tag
> in r? s is not known at compile time so
> the trivial r.age reference is not possible.
> My current solution is:
>
> command = 'v=r.'+s
> execute(command)
> print, 'age=',v
>
> but this is slow because of the run time
> compilation of 'command'. Surely there
> is an easier way ...
>
> Cheers,
> Darran.
>
Sure. Try something like
w = where(tag_names(r) eq strupcase(s))
if (w(0) ne -1) then print, 'age=',r.(w(0))
If you're doing this a lot you'll want to store
the tag_names array somewhere.
Regards,
Tom McGlynn
tam@silk.gsfc.nasa.gov
|
|
|