<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Kyle,<br>
<br>
My understanding is that the list only holds a reference to the
variable, which is why when you delete the variable, the list now
references something that doesn't exist (something that doesn't come
up in Java). I have a utility function I use to create an anonymous
copy to add variables to lists without fear of them being deleted:<br>
<br>
<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>
Then you can just add it to a list like so:<br>
<tt>ListPush( listName, echo(variable) )</tt><br>
<br>
All of the elements in the list will retain all of their metadata
when you pop them off or access them directly via [ ].<br>
<br>
- Walter<br>
<br>
<div class="moz-cite-prefix">On 27-Jan-15 11:13, Kyle Griffin wrote:<br>
</div>
<blockquote
cite="mid:CAKt9o=gM6Gr-YmMao2n6RVTBPARY7jXWEB8knnxYhdV6CzYxWA@mail.gmail.com"
type="cite">
<div dir="ltr">Hi all,
<div><br>
</div>
<div>Some questions as to the interactions between all three of
these (attributes, arrays, and lists) and some discussion as
to the desired functionality of each.</div>
<div><br>
</div>
<div>I've included (in raw text) some code at the end that
demonstrates some of my concerns.</div>
<div><br>
</div>
<div>First: Is it possible for individual elements of an array
to maintain their individual attributes while in an array?
(Parts 1/2 below)</div>
<div>Current practice is to set any individual attributes to the
attributes of the array variable and, when individual elements
are returned, return the elements with all attributes from the
array, even if the attributes have been changed or others
added.</div>
<div><br>
</div>
<div>Secondly, I know Lists are relatively new additions to NCL,
but I'd also like to question how variables are handled when
the lists are being created. (Parts 3/4 below)</div>
<div>It seems that there is not a functional way to create a
list without having a unique variable for each member (e.g.
Part 3). This is a nice workaround to my question above, as
each member keeps its own attributes as well.</div>
<div>However, in order to use these lists in a more practical
sense, the addition of a variable via a loop does *not* work
well, although I believe it is working as designed. When a
variable is added to a list, the original variable must be
maintained in memory and any changes to that variable outside
of the list will be reflected inside the list as well. This
makes sense, as the variable is simply a member of the list -
the list is simply an organizational structure for
pre-existing variables. In order to do this in a loop, one
would need to have a unique name for each variable to be added
to the list OR disassociated the variable that is being added
to the list from its reference variable outside the list. The
unique_string function, while useful for attribute names, does
not work for variable names, e.g.</div>
<div><br>
</div>
<div>varname = unique_string("")</div>
<div>$varname$ = ....data to put in list...</div>
<div><br>
</div>
<div>As a result, lists are essentially useless without a
unique, hand-made variable for each member, something that is
simply not practical in many situations.</div>
<div><br>
</div>
<div>The biggest red flag I've seen, however, is when trying to
add only the value of the variable, via (/.../), to the list.
This would be a potential alternative to creating individual
variables and allowing for subsequent alterations to the
variable in order to add attributes, for example. The
subsequently created list, when popped with ListPop, instantly
seg faults.</div>
<div><br>
</div>
<div>As I write this email, I realize that my Java training from
many years ago comes to mind, especially the ArrayList
functionality. I'm not 100% sure that's what I've tried to
describe in this email, but it might be a start, where
"objects" (any variable with attributes) can be stored (like
lists) but with the index functionality of arrays as well. It
still doesn't get around the "unique name" issue, however.
Thanks for reading...I'm sure these are lofty goals to see any
of this in the future, but the apparent desire to add lists
tells me there is some motivation for this type of data
structure even if the current utility of Lists seems
unintentionally low.</div>
<div><br>
</div>
<div><br>
</div>
<div>Kyle</div>
<div><br>
</div>
<div>test code (on 64-bit RHEL, 6.2.1):</div>
<div>
<div>begin</div>
<div><br>
</div>
<div> a = True</div>
<div> a@thing = 1</div>
<div> a@thing2 = 2</div>
<div> b = True</div>
<div> b@thing = 10</div>
<div> b@thing3 = 20</div>
<div> c = True</div>
<div> c@thing = 100</div>
<div> c@thing2 = 200</div>
<div><br>
</div>
<div> ;Part 1</div>
<div> arraylist = new(3,typeof(a))</div>
<div> arraylist(0) = a</div>
<div> arraylist(1) = b</div>
<div> arraylist(2) = c</div>
<div> print(arraylist(0)) ;expecting value/atts of a</div>
<div> k = arraylist(0)</div>
<div> print(k@thing) ; keeps atts of arraylist, which are
those of c</div>
<div> delete(k)</div>
<div> print("###############################")</div>
<div> ;Part 2</div>
<div> arraylist2 = (/a,b,c/)</div>
<div> print(arraylist(0))</div>
<div> k = arraylist(0)</div>
<div> print(k@thing) ; does same as first example.</div>
<div><br>
</div>
<div> print("###############################")</div>
<div> ;Part 3</div>
<div> thelist = NewList("fifo")</div>
<div> ListPush(thelist,a)</div>
<div> ListPush(thelist,b)</div>
<div> ListPush(thelist,c)</div>
<div> print(thelist) ; keeps values AND attributes</div>
<div><br>
</div>
<div> print("###############################")</div>
<div> ;Part 4</div>
<div> looplist = NewList("fifo")</div>
<div> looplist2 = NewList("fifo")</div>
<div> do i=0,2</div>
<div> thevar = i</div>
<div> thevar@att1 = i*12.0</div>
<div> ListPush(looplist,thevar)</div>
<div> ListPush(looplist2,(/thevar/))</div>
<div> end do</div>
<div> print(looplist)</div>
<div> y=ListPop(looplist)</div>
<div> print("First var from list, should be 0 with att of
0:")</div>
<div> print(y)</div>
<div> print("###############################")</div>
<div> print(looplist2)</div>
<div> print("###############################")</div>
<div> x=ListPop(looplist2) ; SEG FAULT here.</div>
<div> print(x)</div>
<div><br>
</div>
<div>end</div>
</div>
<div>
<div>
<div class="gmail_signature">
<div dir="ltr">----------------------------------------
<div>Kyle S. Griffin</div>
<div>Department of Atmospheric and Oceanic Sciences</div>
<div>University of Wisconsin - Madison</div>
<div>Room 1421</div>
<div>1225 W Dayton St, Madison, WI 53706</div>
<div>Email: <a moz-do-not-send="true"
href="mailto:ksgriffin2@wisc.edu" target="_blank">ksgriffin2@wisc.edu</a></div>
</div>
</div>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
ncl-talk mailing list
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>