# [ncl-talk] Attributes and arrays and lists

Wei Huang huangwei at ucar.edu
Wed Feb 4 08:55:27 MST 2015

```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.
Here, I'd like to show a little bit more on how list can be used.

1. we create a list in two ways:
a. alist = NewList(list_type), where list_type can be "fifo", or
"lifo", we will talk
how list_type is functioned below.
b. create a list directly, use: blist = [/a, b, c/] (where a, b, c are
variables,
which can be in different type).
2. add variables to list
If we create a list using NewList, then there are two ways to add
variables to list.
Assume we have 3 variables, a, b, and c (again, they can be different
type).
a. ListPush, which always add new variable at the head of the list.
ListPush(alist, a)
ListPush(alist, b)
ListPush(alist, c)
After these operations, then alist will be [/c, b, a/].
alist[0] will be c.
b. ListAppend, which always add new variable at the tail of the list.
ListPush(alist, a)
ListPush(alist, b)
ListPush(alist, c)
After these operations, then alist will be [/a, b, c/]
alist[0] will be a.

3. Pop a variable from a list.
How to pop a variable from a list will depends on list_type.
If we have alist as [/c, b, a/].
then list_type = "fifo", will pop a first.
if list_type is "lifo", then pop c first.

Here I have attached two examples, one use ListAppend, and one use ListPush.

Kyle also asked the difference between push a variable and value to a list.

In the code here:
looplist0 = NewList("fifo")
looplist1 = NewList("lifo")
looplist2 = NewList("fifo")
looplist3 = NewList("lifo")

do i=0,2
thevar = i
thevar at att1 = i*12.0
ListPush(looplist0,thevar)
ListPush(looplist1,thevar)
ListPush(looplist2,(/thevar/))
ListPush(looplist3,(/thevar/))
end do

x0=ListPop(looplist0)
print(x0)

x1=ListPop(looplist1)
print(x1)

x2=ListPop(looplist2)
print(x2)

x3=ListPop(looplist3)
print(x3)

looplist0, and looplist1 have variables pushed into.
But as we always push the same variable into the list,
when we do pop, we will get the same variable.
Or here we say variables are pushed into list by reference.

looplist2, and looplist3 have (variable's) value pushed into.
when do x2 = ListPop(looplist2), we pop the value from list,
and assigned the value to variable x2.
Since ever time we pushed a different value to the list,
so the value poped out will not be the same.

Hope this can help people on using list.

Thanks,

Wei

================================================
1850 Table Mesa Dr.
Boulder, CO 80307
Phone: 303-497-8924

On Wed, Jan 28, 2015 at 10:28 AM, Wei Huang <huangwei at ucar.edu> wrote:

> Kyle,
>
> The list looplist2 at the end seg. faulted when doing "ListPop" is
> definitely a bug.
> I have created a JIRA ticket: NCL-2129
> <https://vets.development.ucar.edu/jira/browse/NCL-2129> for this.
>
> I will work on this and report back.
>
> Thanks,
>
> Wei
>
> ================================================
> 1850 Table Mesa Dr.
> Boulder, CO 80307
> Phone: 303-497-8924
>
> On Tue, Jan 27, 2015 at 4:52 PM, Walter Kolczynski <
> walter.kolczynski at noaa.gov> wrote:
>
>>  Kyle,
>>
>> 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:
>>
>> ;
>> ; 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
>>
>> Then you can just add it to a list like so:
>> ListPush( listName, echo(variable) )
>>
>> All of the elements in the list will retain all of their metadata when
>> you pop them off or access them directly via [ ].
>>
>> - Walter
>>
>>
>> On 27-Jan-15 11:13, Kyle Griffin wrote:
>>
>> Hi all,
>>
>>  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.
>>
>>  I've included (in raw text) some code at the end that demonstrates some
>> of my concerns.
>>
>>  First: Is it possible for individual elements of an array to maintain
>> their individual attributes while in an array? (Parts 1/2 below)
>> 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.
>>
>>  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)
>> 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.
>> 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.
>>
>>  varname = unique_string("")
>> \$varname\$ = ....data to put in list...
>>
>>  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.
>>
>>  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.
>>
>>  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.
>>
>>
>>  Kyle
>>
>>  test code (on 64-bit RHEL, 6.2.1):
>>  begin
>>
>>    a = True
>>   a at thing = 1
>>   a at thing2 = 2
>>   b = True
>>   b at thing = 10
>>   b at thing3 = 20
>>   c = True
>>   c at thing = 100
>>   c at thing2 = 200
>>
>>    ;Part 1
>>   arraylist = new(3,typeof(a))
>>   arraylist(0) = a
>>   arraylist(1) = b
>>   arraylist(2) = c
>>   print(arraylist(0)) ;expecting value/atts of a
>>   k = arraylist(0)
>>   print(k at thing) ; keeps atts of arraylist, which are those of c
>>   delete(k)
>>   print("###############################")
>>   ;Part 2
>>   arraylist2 = (/a,b,c/)
>>   print(arraylist(0))
>>   k = arraylist(0)
>>   print(k at thing) ; does same as first example.
>>
>>    print("###############################")
>>   ;Part 3
>>   thelist = NewList("fifo")
>>   ListPush(thelist,a)
>>   ListPush(thelist,b)
>>   ListPush(thelist,c)
>>   print(thelist) ; keeps values AND attributes
>>
>>    print("###############################")
>>   ;Part 4
>>   looplist = NewList("fifo")
>>   looplist2 = NewList("fifo")
>>   do i=0,2
>>     thevar = i
>>     thevar at att1 = i*12.0
>>     ListPush(looplist,thevar)
>>     ListPush(looplist2,(/thevar/))
>>   end do
>>   print(looplist)
>>   y=ListPop(looplist)
>>   print("First var from list, should be 0 with att of 0:")
>>   print(y)
>>   print("###############################")
>>   print(looplist2)
>>   print("###############################")
>>   x=ListPop(looplist2)  ; SEG FAULT here.
>>   print(x)
>>
>>  end
>>   ----------------------------------------
>> Kyle S. Griffin
>> Department of Atmospheric and Oceanic Sciences
>> University of Wisconsin - Madison
>> Room 1421
>> 1225 W Dayton St, Madison, WI 53706
>> Email: ksgriffin2 at wisc.edu
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> 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/20150204/debda2b6/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: append.ncl
Type: application/octet-stream
Size: 515 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150204/debda2b6/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: push.ncl
Type: application/octet-stream
Size: 509 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150204/debda2b6/attachment-0001.obj
```

More information about the ncl-talk mailing list