[ncl-talk] Retrieving variables from lists.

Alan Brammer abrammer at albany.edu
Mon Aug 4 19:27:17 MDT 2014


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
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20140804/3c3a8c73/attachment.html 


More information about the ncl-talk mailing list