[ncl-talk] Retrieving variables from lists.

David Brown dbrown at ucar.edu
Tue Aug 5 14:21:50 MDT 2014


Hi Alan,
We do see that ListIndex is currently limited to indexing variables within
the current scope. I agree that it would be a good idea to allow for
indexing based on the symbol name as a string. We will add a ticket for
this feature. Our current thinking is that we will allow the ListIndex
function to accept strings as well as actual variable references unless we
encounter unforeseen difficulties with that approach. Of course, one might
want to index on variable values as well, in which case, there could be a
conflict between the string name of a variable and a string value. So it's
possible we may need more than one function.
 -dave



On Mon, Aug 4, 2014 at 7:27 PM, Alan Brammer <abrammer at albany.edu> wrote:

> Spoke with Kyle offline; just for everyone else while this is useful info
> it does not address my issue.
>
> The example shows that x is not defined in the main body so ListIndex does
> not work. The two instances of x in the list have different values as
> desired but how can one retrieve them without exactly knowing their
> location.
>
> For now I am attaching my own naming attribute to each variable to then
> identify list items later on.
>
> Thanks,
> Alan.
>
>
>
> On 4 August 2014 17:57:13 GMT-04:00, Kyle Griffin <ksgriffin2 at wisc.edu>
> wrote:
>>
>> So are you referencing the ListIndex function?
>>
>> https://www.ncl.ucar.edu/Document/Functions/Built-in/ListIndex.shtml
>>
>> From the list section of the manual...
>> https://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml#ListVariables
>>
>> I usually try to construct lists with ListPush and ListPop...this is more
>> consistent with how lists are intended to work in other languages (I know
>> it from Java, but I'm sure there are others) where they are intended to
>> host any number of data types, but the *order* of the items is what is
>> relevant. You declare the list to be "first-in, first out" or "last-in,
>> first out" and push things in or pop things out of the list, which will
>> return items as intended. You can also get the index and pull it out as an
>> object from an array would as well, although this is not as simple.
>>
>> Personally, I've had a few issues with how variables are treated once
>> pushed/popped from a list last time I tried to use the feature, but I can't
>> recall what the specific issue was off the top of my head. I'd be curious
>> as well to hear more from those who work with lists in NCL more often.
>>
>>
>> Kyle
>>
>> ----------------------------------------
>> 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
>>
>>
>> On Mon, Aug 4, 2014 at 4:26 PM, Brammer, Alan P <abrammer at albany.edu>
>> wrote:
>>
>>> Dear NCL talk,
>>>
>>> I never really utilise lists in NCL as I struggle with how they work.
>>>  Hopefully this mundane example can help clarify my desires and maybe find
>>> a solution.  Or maybe I’m trying to use them in a way they were not meant.
>>>
>>> I have a function “list_test” as below, and in my script I call that and
>>> depending on the argument will get a return list back, that all works
>>> great.  My question is how do I get say variable "x" out of the returned
>>> list.  I can assume its location in the list but that seems risky and
>>> unfriendly for development.
>>>
>>> Can I get an array of the variables names within a list so I can then
>>> pull “x” out using ListIndex or something similar.
>>> Is there a listIndex where I pass the variable name instead of the
>>> variable.
>>> Do I have to attach a list of names and index locations to the list so I
>>> know where things are in it?
>>>
>>>
>>> As always these example scripts are exemplifying much more complicated
>>> code therefore hard coding the answer isn’t really the solution I want.
>>>
>>>
>>> Thanks in advance,
>>>
>>> Alan.
>>>
>>>
>>>
>>>
>>> undef("list_test")
>>> function list_test(a:logical)
>>> local k, x, y, x, retlist
>>> begin
>>>   if(a)
>>>       x = fspan(0,10, 11)
>>>       y = fspan(100,120,11)   ;  just random arrays of numbers.
>>>       z = ispan(0,100,1)
>>>       retlist = [/ x, y, z /]
>>>   else
>>>       k  = ispan(10,15, 1)
>>>       x = fspan(10,20, 11)
>>>       y = fspan(200,220,11)
>>>       retlist = [/ k, x, y /]
>>>   end if
>>>
>>>   return(retlist)
>>> end
>>>
>>>
>>>
>>>
>>> begin
>>>
>>> firstlist = list_test(True)
>>> print(firstlist)
>>>
>>> ; xind = ind( firstlist .eq. “x”   )   ;;; is there some way for this to
>>> work?  firstlist at variable ? ?
>>>
>>> secondlist = list_test(False)
>>> print(secondlist)
>>>
>>> ; xind = ind( secondlist .eq. “x”   )   ;;;
>>>
>>> end
>>>
>>> _______________________________________________
>>> 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/20140805/afc5c3e1/attachment.html 


More information about the ncl-talk mailing list