<div dir="ltr">Hi Wei,<div><br></div><div>It looks like the way I had hoped to use lists was always there, or at least intended to be there. Looking forward to seeing the fix where we can push unnamed variables into lists without segfaults (as your wonderful examples continue to do, of course).</div><div><br></div><div>Thanks also for the overview - some of the capabilities here are really nice to have and I'm sure the general awareness of the capabilities of List is not (yet!) as wide as it probably should be. Hopefully this helps others on the list as well.</div><div><br></div><div>Thanks again for the info and the upcoming fix.</div><div><br></div><div><br></div><div>Kyle</div></div><div class="gmail_extra"><br clear="all"><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 href="mailto:ksgriffin2@wisc.edu" target="_blank">ksgriffin2@wisc.edu</a></div></div></div></div>
<br><div class="gmail_quote">On Wed, Feb 4, 2015 at 9:55 AM, Wei Huang <span dir="ltr"><<a href="mailto:huangwei@ucar.edu" target="_blank">huangwei@ucar.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif">Few days ago, Kyle Griffin asked some questions about list, and reported a seg. fault issue. Now we have fixed the seg. fault issue, and the fix will be in the new release.</div><div class="gmail_default" style="font-family:verdana,sans-serif">Here, I'd like to show a little bit more on how list can be used.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">1. we create a list in two ways:</div><div class="gmail_default" style="font-family:verdana,sans-serif"> a. alist = NewList(list_type), where list_type can be "fifo", or "lifo", we will talk</div><div class="gmail_default" style="font-family:verdana,sans-serif"> how list_type is functioned below.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> b. create a list directly, use: blist = [/a, b, c/] (where a, b, c are variables,</div><div class="gmail_default" style="font-family:verdana,sans-serif"> which can be in different type).</div><div class="gmail_default" style="font-family:verdana,sans-serif">2. add variables to list</div><div class="gmail_default" style="font-family:verdana,sans-serif"> If we create a list using NewList, then there are two ways to add variables to list.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> Assume we have 3 variables, a, b, and c (again, they can be different type).</div><div class="gmail_default" style="font-family:verdana,sans-serif"> a. ListPush, which always add new variable at the head of the list.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> ListPush(alist, a)</div><div class="gmail_default" style="font-family:verdana,sans-serif"> ListPush(alist, b)</div><div class="gmail_default" style="font-family:verdana,sans-serif"> ListPush(alist, c)</div><div class="gmail_default" style="font-family:verdana,sans-serif"> After these operations, then alist will be [/c, b, a/].</div><div class="gmail_default" style="font-family:verdana,sans-serif"> alist[0] will be c.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> b. ListAppend, which always add new variable at the tail of the list.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> ListPush(alist, a)</div><div class="gmail_default" style="font-family:verdana,sans-serif"> ListPush(alist, b)</div><div class="gmail_default" style="font-family:verdana,sans-serif"> ListPush(alist, c)</div><div class="gmail_default" style="font-family:verdana,sans-serif"> After these operations, then alist will be [/a, b, c/]</div><div class="gmail_default" style="font-family:verdana,sans-serif"> alist[0] will be a.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">3. Pop a variable from a list.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> How to pop a variable from a list will depends on list_type.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> If we have alist as [/c, b, a/].</div><div class="gmail_default" style="font-family:verdana,sans-serif"> then list_type = "fifo", will pop a first.</div><div class="gmail_default" style="font-family:verdana,sans-serif"> if list_type is "lifo", then pop c first.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Here I have attached two examples, one use ListAppend, and one use ListPush.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Kyle also asked the difference between push a variable and value to a list.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">In the code here:</div><div class="gmail_default"><div class="gmail_default"><font face="verdana, sans-serif">looplist0 = NewList("fifo")</font></div><div class="gmail_default"><font face="verdana, sans-serif">looplist1 = NewList("lifo")</font></div><span class=""><div class="gmail_default"><font face="verdana, sans-serif">looplist2 = NewList("fifo")</font></div></span><div class="gmail_default"><font face="verdana, sans-serif">looplist3 = NewList("lifo")</font></div><span class=""><div class="gmail_default"><font face="verdana, sans-serif"><br></font></div><div class="gmail_default"><font face="verdana, sans-serif">do i=0,2</font></div><div class="gmail_default"><font face="verdana, sans-serif"> thevar = i</font></div><div class="gmail_default"><font face="verdana, sans-serif"> thevar@att1 = i*12.0</font></div></span><div class="gmail_default"><font face="verdana, sans-serif"> ListPush(looplist0,thevar)</font></div><div class="gmail_default"><font face="verdana, sans-serif"> ListPush(looplist1,thevar)</font></div><div class="gmail_default"><font face="verdana, sans-serif"> ListPush(looplist2,(/thevar/))</font></div><div class="gmail_default"><font face="verdana, sans-serif"> ListPush(looplist3,(/thevar/))</font></div><div class="gmail_default"><font face="verdana, sans-serif">end do</font></div><div class="gmail_default"><font face="verdana, sans-serif"><br></font></div><div class="gmail_default"><font face="verdana, sans-serif">x0=ListPop(looplist0)</font></div><div class="gmail_default"><font face="verdana, sans-serif">print(x0)</font></div><div class="gmail_default"><font face="verdana, sans-serif"><br></font></div><div class="gmail_default"><font face="verdana, sans-serif">x1=ListPop(looplist1)</font></div><div class="gmail_default"><font face="verdana, sans-serif">print(x1)</font></div><div class="gmail_default"><font face="verdana, sans-serif"><br></font></div><div class="gmail_default"><font face="verdana, sans-serif">x2=ListPop(looplist2)</font></div><div class="gmail_default"><font face="verdana, sans-serif">print(x2)</font></div><div class="gmail_default"><font face="verdana, sans-serif"><br></font></div><div class="gmail_default"><font face="verdana, sans-serif">x3=ListPop(looplist3)</font></div><div class="gmail_default"><font face="verdana, sans-serif">print(x3)</font></div><div style="font-family:verdana,sans-serif"><br></div><div style="font-family:verdana,sans-serif"><br></div><div style="font-family:verdana,sans-serif">looplist0, and looplist1 have variables pushed into.</div><div style="font-family:verdana,sans-serif">But as we always push the same variable into the list,</div><div style="font-family:verdana,sans-serif">when we do pop, we will get the same variable.</div><div style="font-family:verdana,sans-serif">Or here we say variables are pushed into list by reference.</div><div style="font-family:verdana,sans-serif"><br></div><div style="font-family:verdana,sans-serif">looplist2, and looplist3 have (variable's) value pushed into.</div><div style="font-family:verdana,sans-serif">when do x2 = ListPop(looplist2), we pop the value from list,</div><div style="font-family:verdana,sans-serif">and assigned the value to variable x2.</div><div style="font-family:verdana,sans-serif">Since ever time we pushed a different value to the list,</div><div style="font-family:verdana,sans-serif">so the value poped out will not be the same.</div><div style="font-family:verdana,sans-serif"><br></div><div style="font-family:verdana,sans-serif">Hope this can help people on using list.</div><div style="font-family:verdana,sans-serif"><br></div><div style="font-family:verdana,sans-serif">Thanks,</div><div style="font-family:verdana,sans-serif"><br></div><div style="font-family:verdana,sans-serif">Wei</div></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div></div><div class="gmail_extra"><span class=""><br clear="all"><div><div><div dir="ltr"><font face="tahoma, sans-serif">================================================</font><div><font face="tahoma, sans-serif">1850 Table Mesa Dr.</font></div><div><font face="tahoma, sans-serif">Boulder, CO 80307</font></div><div><font face="tahoma, sans-serif">Phone: <a href="tel:303-497-8924" value="+13034978924" target="_blank">303-497-8924</a></font></div></div></div></div>
<br></span><div><div class="h5"><div class="gmail_quote">On Wed, Jan 28, 2015 at 10:28 AM, Wei Huang <span dir="ltr"><<a href="mailto:huangwei@ucar.edu" target="_blank">huangwei@ucar.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif">Kyle,</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">The list looplist2 at the end seg. faulted when doing "ListPop" is definitely a bug.</div><div class="gmail_default" style="font-family:verdana,sans-serif">I have created a JIRA ticket: <a href="https://vets.development.ucar.edu/jira/browse/NCL-2129" rel="92414" style="font-family:Arial,sans-serif;font-size:14px;line-height:20px;color:rgb(59,115,175);text-decoration:none" target="_blank">NCL-2129</a> for this.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">I will work on this and report back.</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Thanks,</div><div class="gmail_default" style="font-family:verdana,sans-serif"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif">Wei</div></div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><font face="tahoma, sans-serif">================================================</font><div><font face="tahoma, sans-serif">1850 Table Mesa Dr.</font></div><div><font face="tahoma, sans-serif">Boulder, CO 80307</font></div><div><font face="tahoma, sans-serif">Phone: <a href="tel:303-497-8924" value="+13034978924" target="_blank">303-497-8924</a></font></div></div></div></div><div><div>
<br><div class="gmail_quote">On Tue, Jan 27, 2015 at 4:52 PM, Walter Kolczynski <span dir="ltr"><<a href="mailto:walter.kolczynski@noaa.gov" target="_blank">walter.kolczynski@noaa.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div 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<div><div><br>
<br>
<div>On 27-Jan-15 11:13, Kyle Griffin wrote:<br>
</div>
</div></div><blockquote type="cite"><div><div>
<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>
<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 href="mailto:ksgriffin2@wisc.edu" target="_blank">ksgriffin2@wisc.edu</a></div>
</div>
</div>
</div>
</div>
</div>
<br>
<fieldset></fieldset>
<br>
</div></div><pre>_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a>
</pre>
</blockquote>
<br>
</div>
<br>_______________________________________________<br>
ncl-talk mailing list<br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div></div>
<br>_______________________________________________<br>
ncl-talk mailing list<br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>