[Q] extracting value from a structure ... [message #12793] |
Thu, 10 September 1998 00:00  |
dEdmundson
Messages: 23 Registered: February 1998
|
Junior Member |
|
|
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.
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
|
|
|
|
Re: [Q] extracting value from a structure ... [message #12923 is a reply to message #12793] |
Fri, 11 September 1998 00:00  |
Imanol Echave
Messages: 26 Registered: May 1998
|
Junior Member |
|
|
What do you think about this?
----------------------------------------------
struct={NAME:'Imanol',AGE:24}
field='AGE'
index=(WHERE(field EQ TAG_NAMES(struct)))[0]
value=struct.(index)
PRINT,field+'=',value
----------------------------------------------
NOTE: TAG_NAMES returns the names ALWAYS ON UPPERCASE. If you do field='age' the
program will not work.
|
|
|