[ncl-talk] dynamic variable names
Walter Kolczynski
walter.kolczynski at noaa.gov
Thu Feb 4 12:28:16 MST 2016
If you want to do something fancier (for instance, you need to keep the
variable metadata around), you can fake an associative array
(hashtable/dictionary) by creating parallel lists instead of as
attributes, and ListIndexFromName.
One thing to note about lists is that it doesn't create a copy of the
variable, so if you delete the variable the list can't read it. This can
be really inconvenient if you want to build a list in a loop, so I use
the following function to produce an anonymous copy of the variable:
;
; Creates an anonymous copy of a variable for inclusion in lists without
causing issues if the variable is deleted.
;
if isdefined("echo") then undef("echo") end if
function echo(variable)
local variable2
begin
variable2 = variable
return variable2
end ; echo
Also, because the association between the two lists is the important
part, you have to be careful to never modify one without modifying the
other. A full(-ish) example might look something like this (I haven't
tested this code; may contain syntax errors):
filein = open(filename, "r") ; open a file
varNames = getfilevarnames(filein) ; get the variable names
varList = NewList("fifo") ; create a new listfor var data
varNameList = NewList("fifo") ; create a new list for var names
; read in all the variable in the file
do v=0, dimsizes(varNames)-1
variable = filein->$varNames(v)$ ; read variable
from file
; Push the variable and variable name onto their respective lists.
; Since the return value from echo is used directly without
assigning it to a
; variable, it can't be deleted unless we intentionally remove it
from the list.
ListPush( varList, echo(variable) )
ListPush( varNameList, echo( varNames(v) ) )
delete(variable) ; delete the variable
end do
; get user input
varInd = ListIndexFromName(varNameList, requestedVarName)
if(varInd.eq.-1) then ; Make sure we found the variable name
print("ERROR: Variable " + requestedVarName+ " not found")
return
end if
variable = varList[varInd]
; do stuff with variable
- Walter
On 2016-02-04 09:59, Milinski, Sebastian wrote:
> Hi Rick,
>
> That helped a lot, thank you!
>
> Sebastian
>
>
>> On 04 Feb 2016, at 13:43, Rick Brownrigg <brownrig at ucar.edu> wrote:
>>
>> Hi Sebastian,
>>
>> As you point out, it can't be done directly with variables and $..$
>> notation. But it would work if your variables were made to be
>> attributes of some "hosting variable":
>>
>> ncl 0> hostVar = True
>> ncl 1> hostVar at ALLindex = 3.14159
>> ncl 2> user_selection = "ALL"
>> ncl 3> dynvar = user_selection + "index"
>> ncl 4> var1 = hostVar@$dynvar$
>> ncl 5> print(var1)
>>
>> Variable: var1
>> Type: float
>> Total Size: 4 bytes
>> 1 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [1]
>> Coordinates:
>> (0) 3.14159
>>
>> I hope that helps...
>> Rick
>>
>>
>> On Thu, Feb 4, 2016 at 3:30 AM, Milinski, Sebastian
>> <sebastian.milinski at mpimet.mpg.de> wrote:
>>
>> Hi,
>>
>> I would like to access a variable. The name of the variable to be
>> accessed is created from user input (string) and a suffix (string)
>>
>> user_selection = "ALL"
>> dynvar = user_selection + "index"
>> var1 = dynvar ; <- here I
>> do not want the string ALLindex but the content of ALLindex.
>> print(var1)
>>
>>
>> The syntax var1 = $dynvar$ does not work. Is there an easy way to
>> do this without several if-statements?
>>
>> Thanks,
>> Sebastian
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160204/4229e4e6/attachment.html
More information about the ncl-talk
mailing list