Re: selecting a variable based on a variable name

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Tue, 04 Aug 2009 17:16:44 -0600

Not ideal but the following may do what you want

function get_critical(N)
local p75, p90, p95
begin
 p75 = 0.8
 p90 = 0.83
 p95 = 0.87

 if (isvar("p"+N)) then
     if (N.eq.75) then
         return(p75)
     end if
     if (N.eq.90) then
         return(p90)
     end if
     if (N.eq.95) then
         return(p95)
     end if
 end if

 return(-999)
end

  N=90
  print( get_critical(N) )

Mark.Collier_at_csiro.au wrote:
> Hi Dennis,
>
> this will only give me the new string not the value attached to a variable called "p95"
> it doesn't really matter as there are other work-arounds but usually there is something like an eval command to make these kinds of assignments easy.
>
> Regards,
> ________________________________________
> From: Dennis Shea [shea_at_ucar.edu]
> Sent: Tuesday, 4 August 2009 10:09 PM
> To: Collier, Mark (CMAR, Aspendale)
> Cc: ncl-talk_at_ucar.edu
> Subject: Re: selecting a variable based on a variable name
>
> critical = "p" + N
>
> or , (say)
>
> critical = "p" + sprinti("%0.3i", N)
> http://www.ncl.ucar.edu/Document/Functions/Built-in/sprinti.shtml
>
> Mark.Collier_at_csiro.au wrote:
>
>> Hi,
>>
>> p75 = 0.8
>> p90 = 0.83
>> p95 = 0.87
>>
>> N=90
>>
>> how can i assign the p90 value (0.83) to a variable critical using just the string "p" and integer N?
>>
>> this doesn't work?
>>
>> critical="p"+$N$
>>
>> print(critical)
>>
>> Regards,
>> Mark.
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>

-- 
======================================================
Dennis J. Shea                  tel: 303-497-1361    |
P.O. Box 3000                   fax: 303-497-1333    |
Climate Analysis Section                             |
Climate & Global Dynamics Div.                       |
National Center for Atmospheric Research             |
Boulder, CO  80307                                   |
USA                        email: shea 'at' ucar.edu |
======================================================
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Aug 04 2009 - 17:16:44 MDT

This archive was generated by hypermail 2.2.0 : Wed Aug 05 2009 - 20:36:03 MDT