Re: Alternative to EXECUTE? [message #37190] |
Tue, 02 December 2003 07:04 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Anne Martel writes:
> I've just realised that the IDL Virtual Machine won't work with
> EXECUTE. Does anyone have an alternative for the sort of use described
> below?
>
> I'm using EXECUTE to initialise an object with a set of fields defined
> by the structure info. The number of fields defined in info will vary
> depending on the type of image I'm reading in so I only want to set a
> subset of the fields. I could explicitity test for each tag name but
> that makes it harder to add new fields later and I would end up with a
> very long CASE statement.
>
> pro QmcImage::set_info,info
> tmp_class = {QmcImage}
> object_fields = Tag_Names(tmp_class)
> info_fields = Tag_Names(info)
> num=n_elements(info_fields)
> for i = 0,num-1 do begin
> index = where(strcmp( info_fields[i], object_fields,/F),count)
> if(count eq 1) then begin
> str='self.' + info_fields[i] + '= info.' + info_fields[i]
> ok = execute(str)
> endif
> endfor
> end
I think I would write the loop like this:
FOR j=0,num-1 DO BEGIN
index = Where(object_fields EQ info_fields[j], count)
IF count EQ 1 THEN self.(index) = info.(j)
ENDFOR
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|