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 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: How do you access a field in a struct? [message #18321] Wed, 15 December 1999 00:00
Karri Kaksonen is currently offline  Karri Kaksonen
Messages: 27
Registered: October 1999
Junior Member
Thank you Martin,

I had never used the WHERE function before.
That was a neat solution.

--
Regards,

Karri Kaksonen
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 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 [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
Re: How do you access a field in a struct? [message #18323 is a reply to message #18321] Wed, 15 December 1999 00:00 Go to previous message
Volker.Mertens is currently offline  Volker.Mertens
Messages: 1
Registered: December 1999
Junior Member
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 begin
i = execute('b_num = b.' + selection[i])
; if i ne 0 then ...
totalsum=totalsum + a.(i) * b_num
;>totalsum=totalsum + a.(i) * b.(selection[i])
> print, totalsum
> end
>
However, you should check if b really contains all a tags.


--
Volker Mertens email: Volker.Mertens@dlr.de
German Aerospace Center (DLR) Tel: +49 (0)30 67055 334
Rutherfordstr. 2 Fax: +49 (0)30 67055 386
12489 Berlin http://www.ba.dlr.de/NE-PE/
Germany
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Spatial normalisation
Next Topic: Changing elements in structures

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

Current Time: Wed Oct 08 19:25:00 PDT 2025

Total time taken to generate the page: 0.00593 seconds