comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » [Q] extracting value from a structure ...
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
[Q] extracting value from a structure ... [message #12793] Thu, 10 September 1998 00:00 Go to next message
dEdmundson is currently offline  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 #12794 is a reply to message #12793] Wed, 09 September 1998 00:00 Go to previous messageGo to next message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Darran Edmundson (dEdmundson@Bigfoot.com) writes:

> 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?

I would try something like this:

r = {name: 'john', age:18}
s = 'age'
index = Where(Tag_Names(r) EQ StrUpCase(s), count)
IF count NE 0 THEN Print, r.(index[0])

Cheers,

David
----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Re: [Q] extracting value from a structure ... [message #12923 is a reply to message #12793] Fri, 11 September 1998 00:00 Go to previous message
Imanol Echave is currently offline  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.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: xwindows colour issues
Next Topic: call_external won't release dll

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 20:06:39 PDT 2025

Total time taken to generate the page: 0.05828 seconds