[ncl-talk] converting string into a variable name

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Thu Jan 26 07:15:13 MST 2023


Tabish, for your example, you could use a simple array instead of
individually named variables.

y = new (20, integer)
y(1) = ispan(0,1000,1)
y(2) = ispan(1000,2000,1)
y(3) = ispan(2000,3000,1)
--- etc. ---

do i = 1,19
 M = max(y(i))
 m = min(y(i))
--- etc. ---

Note that NCL uses zero-based arrays, so this example ignores y(0) to keep
your original indexing scheme from 1 to 19.


On Thu, Jan 26, 2023 at 9:09 AM Dave Allured - NOAA Affiliate <
dave.allured at noaa.gov> wrote:

> Tabish, NCL string references work for file variables, but not for
> in-memory variables.  See NCL reference manual, section "String references".
>
>
> On Thu, Jan 26, 2023 at 8:46 AM Tabish Ansari via ncl-talk <
> ncl-talk at mailman.ucar.edu> wrote:
>
>> Hi
>>
>> I have several systematically named variables that I want to refer to by
>> generating the corresponding string via a loop. I'm able to generate a
>> string that exactly matches the variable name but I'm unable to perform any
>> operations over the variables by referring to the string.
>>
>> Here's my code:
>>
>> begin
>> y1 = ispan(0,1000,1)
>> y2 = ispan(1000,2000,1)
>> y3 = ispan(2000,3000,1)
>> y4 = ispan(3000,4000,1)
>> y5 = ispan(4000,5000,1)
>> y6 = ispan(5000,6000,1)
>> y7 = ispan(6000,7000,1)
>> y8 = ispan(7000,8000,1)
>> y9 = ispan(8000,9000,1)
>> y10 = ispan(9000,10000,1)
>> y11 = ispan(10000,11000,1)
>> y12 = ispan(11000,12000,1)
>> y13 = ispan(12000,13000,1)
>> y14 = ispan(13000,14000,1)
>> y15 = ispan(14000,15000,1)
>> y16 = ispan(15000,16000,1)
>> y17 = ispan(16000,17000,1)
>> y18 = ispan(17000,18000,1)
>> y19 = ispan(18000,19000,1)
>>
>> do i = 1,19
>>  var = "y"+tostring(i)
>>  M = max($var$)
>>  m = min($var$)
>>  print(M+"       "+m)
>> end do
>>
>> end
>>
>> I get a syntax error with this. Is it possible to achieve the same thing
>> through some other syntax in an efficient way?
>>
>> Thanks a lot.
>>
>> Tabish
>>
>>
>> -------------------------------
>> Dr Tabish Ansari
>> Research Associate
>> Air Quality Modelling Group
>> IASS-Potsdam
>> Germany
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230126/9994dd89/attachment.htm>


More information about the ncl-talk mailing list