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

Home » Public Forums » archive » Re: How do you access a field in a struct?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: How do you access a field in a struct? [message #18322 is a reply to message #18321] Wed, 15 December 1999 00:00 Go to previous messageGo to previous message
m218003 is currently offline  m218003
Messages: 56
Registered: August 1999
Member
In article <38573DF7.39E554F9@twins.dna.fi>,
Karri Kaksonen <karri@twins.dna.fi> writes:
> Hi,
>
> I have a small problem with accessing my structs.
>
> The complete program (that does not work) looks like:
> pro beer
> a = { $
> beer: 1, $
> icecream: 2 $
> }
>
> b = { $
> milk: 0.60, $
> beer: 1.25, $
> icecream: 0.75, $
> tea: 0.50 $
> }
>
> totalsum = 0.0
> selection = tag_names(a)
> for i=0,n_tags(a)-1 do totalsum=totalsum + a.(i) * b.(selection)
> print, totalsum
> end
>
> When I run this program I get:
> IDL> beer
> % Type conversion error: Unable to convert given STRING to Long.
> % Detected at: BEER 16 C:\RSI\IDL52\mri\beer.pro
> % Type conversion error: Unable to convert given STRING to Long.
> % Detected at: BEER 16 C:\RSI\IDL52\mri\beer.pro
> 1.80000
>
> The funny thing is that it obviously replaced "beer" and "icecream"
> with "milk" as it could not understand that b.('beer') is the same
> as b.beer
>

Sure enough, IDL complains. Insert a
HELP, selection
statement, and you'll see that selection is of type string.
The "tag selector" b.(i) requirs an integer, however. The solution to your
problem is the WHERE function. The core statement is
bind = WHERE(tag_names(b) eq selection[i])
However, for reasons of efficiency and user-tampering-protection, the code
would rather look like:

totalsum = 0.0
selection = tag_names(a)
bnames = tag_names(b)
for i=0,n_tags(a)-1 do begin
bind = WHERE(bnames eq selection[i],count)
if (count eq 1) then begin
totalsum=totalsum + a.(i) * b.(bind[0])
endif else begin
print,'No matching tag in b for ',selection[i]
endelse
endfor
print, totalsum

BTW: the answer is 2.75 ;-)

Cheers,
Martin

--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie [[
[[ Bundesstr. 55, 20146 Hamburg [[
[[ phone: +49 40 41173-308 [[
[[ fax: +49 40 41173-298 [[
[[ martin.schultz@dkrz.de [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Spatial normalisation
Next Topic: Changing elements in structures

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

Current Time: Sun Oct 12 19:02:10 PDT 2025

Total time taken to generate the page: 0.80002 seconds