<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
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.<br>
<br>
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:<br>
<tt><br>
</tt><tt> </tt><tt>;</tt><tt><br>
</tt><tt>; Creates an anonymous copy of a variable for inclusion in
lists without causing issues if the variable is deleted.</tt><tt><br>
</tt><tt>;</tt><tt><br>
</tt><tt>if isdefined("echo") then undef("echo") end if</tt><tt><br>
</tt><tt>function echo(variable)</tt><tt><br>
</tt><tt> local variable2</tt><tt><br>
</tt><tt> begin</tt><tt><br>
</tt><tt> variable2 = variable</tt><tt><br>
</tt><tt> return variable2</tt><tt><br>
</tt><tt> end ; echo</tt><br>
<br>
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):<br>
<tt><br>
</tt><tt>filein = open(filename, "r")</tt><tt> ; open a file<br>
</tt><tt>varNames = getfilevarnames(filein) ; get the variable
names</tt><tt><br>
</tt><tt><br>
</tt><tt>varList = NewList("fifo") ; create a new list</tt><tt>
for var data<br>
varNameList = NewList("fifo") ; create a new list for var
names<br>
</tt><tt><br>
; read in all the variable in the file<br>
</tt><tt>do v=0, dimsizes(varNames)-1</tt><tt><br>
</tt><tt> variable = filein->$varNames(v)$ ;
read variable from file</tt><tt><br>
<br>
</tt><tt> ; Push the variable and variable name onto their
respective lists.<br>
; </tt><tt><tt>Since the return value from echo is used
directly without assigning it to a<br>
; variable, it can't be deleted unless we intentionally
remove it from the list.<br>
</tt> ListPush( varList, echo(variable) )
<br>
ListPush( varNameList, echo( varNames(v) ) )<br>
<br>
delete(variable) ; delete the
variable<br>
</tt><tt>end do</tt><tt><br>
</tt><tt><br>
</tt><tt>; get user input</tt><tt><br>
</tt><tt><br>
</tt><tt>varInd = ListIndexFromName(varNameList, requestedVarName)</tt><tt>
<br>
</tt><tt>if(varInd.eq.-1) then</tt><tt> ; Make sure we
found the variable name<br>
</tt><tt> print("ERROR: Variable " + </tt><tt><tt>requestedVarName</tt>+
" not found")</tt><tt><br>
</tt><tt> return</tt><tt> </tt><tt><br>
</tt><tt>end if<br>
</tt><br>
<tt>variable = varList[varInd]</tt><tt><br>
</tt><tt>; do stuff with variable</tt><tt><br>
</tt><tt><br>
</tt>- Walter<br>
<br>
<div class="moz-cite-prefix">On 2016-02-04 09:59, Milinski,
Sebastian wrote:<br>
</div>
<blockquote
cite="mid:191EB3B4-24F1-4681-8DC3-B774B65B8064@mpimet.mpg.de"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
Hi Rick,
<div class=""><br class="">
</div>
<div class="">That helped a lot, thank you!</div>
<div class=""><br class="">
</div>
<div class="">Sebastian</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">
<div>
<blockquote type="cite" class="">
<div class="">On 04 Feb 2016, at 13:43, Rick Brownrigg <<a
class="moz-txt-link-abbreviated"
href="mailto:brownrig@ucar.edu"><a class="moz-txt-link-abbreviated" href="mailto:brownrig@ucar.edu">brownrig@ucar.edu</a></a>>
wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">
<div class="">
<div class="">
<div class="">Hi Sebastian,<br class="">
<br class="">
</div>
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":<br class="">
<br class="">
ncl 0> hostVar = True<br class="">
ncl 1> hostVar@ALLindex = 3.14159<br class="">
ncl 2> user_selection = "ALL"<br class="">
ncl 3> dynvar = user_selection + "index"<br
class="">
ncl 4> var1 = hostVar@$dynvar$<br class="">
ncl 5> print(var1)<br class="">
<br class="">
Variable: var1<br class="">
Type: float<br class="">
Total Size: 4 bytes<br class="">
1 values<br class="">
Number of Dimensions: 1<br class="">
Dimensions and sizes: [1]<br class="">
Coordinates:<br class="">
(0) 3.14159<br class="">
<br class="">
</div>
I hope that helps...<br class="">
</div>
Rick<br class="">
<br class="">
</div>
<div class="gmail_extra"><br class="">
<div class="gmail_quote">On Thu, Feb 4, 2016 at 3:30 AM,
Milinski, Sebastian <span dir="ltr" class=""><<a
class="moz-txt-link-abbreviated"
href="mailto:sebastian.milinski@mpimet.mpg.de"><a class="moz-txt-link-abbreviated" href="mailto:sebastian.milinski@mpimet.mpg.de">sebastian.milinski@mpimet.mpg.de</a></a>></span>
wrote:<br class="">
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br
class="">
<br class="">
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)<br class="">
<br class="">
user_selection = "ALL"<br class="">
dynvar = user_selection + "index"<br
class="">
var1 = dynvar
; <- here I do not want the
string ALLindex but the content of ALLindex.<br
class="">
print(var1)<br class="">
<br class="">
<br class="">
The syntax var1 = $dynvar$ does not work. Is there
an easy way to do this without several
if-statements?<br class="">
<br class="">
Thanks,<br class="">
Sebastian<br class="">
<br class="">
<br class="">
_______________________________________________<br
class="">
ncl-talk mailing list<br class="">
<a moz-do-not-send="true"
href="mailto:ncl-talk@ucar.edu" class="">ncl-talk@ucar.edu</a><br
class="">
List instructions, subscriber options, unsubscribe:<br
class="">
<a moz-do-not-send="true"
href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk"
rel="noreferrer" target="_blank" class="">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br
class="">
<br class="">
</blockquote>
</div>
<br class="">
</div>
_______________________________________________<br
class="">
ncl-talk mailing list<br class="">
<a moz-do-not-send="true" href="mailto:ncl-talk@ucar.edu"
class="">ncl-talk@ucar.edu</a><br class="">
List instructions, subscriber options, unsubscribe:<br
class="">
<a class="moz-txt-link-freetext"
href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br
class="">
</div>
</blockquote>
</div>
<br class="">
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
ncl-talk mailing list
<a class="moz-txt-link-abbreviated" href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a>
List instructions, subscriber options, unsubscribe:
<a class="moz-txt-link-freetext" href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a>
</pre>
</blockquote>
<br>
</body>
</html>