[Gretl-users] Access elements of a list

Allin Cottrell cottrell at wfu.edu
Thu Jul 25 18:25:57 EDT 2013

On Thu, 25 Jul 2013, Logan Kelly wrote:

> I have two questions. First, is this the correct list to 
> post this question? I am not sure if it belongs here or in 
> the gretl-devl list. Apologies, if I have guessed wrongly.

Could go either way, no worries.

> Second, regarding accessing a list element. Should following 
> assign the data in the first series of lstX to matrix matX?
> matrix matX = {lstX[1]}
> Currently, this yields the matX = series id number. This 
> question is related to an earlier question on the list see 
> below.

That's what I'd expect. A named list in gretl is an array of 
ID numbers of series in the current dataset. Let's look at the 
related case you mention:

>>> Is it possible to access elements of a list. I need the variable name-as a
>>> string-of the i_th element of a list. Something like
>>> string variable_name = varnam(ylist[i])
>> You can't directly index into a list [...]
> Actually, that seems a bit lame. Now in CVS and snapshots you
> can do that -- the expression above should now work (apart
> from the typo of "varnam" for "varname" ;-).

The help doc says of the varname() function: "If given an 
integer argument [v], returns the name of the variable with ID 
number v, or generates an error if there is no such variable." 
The expression "ylist[i]", for ylist a named list and i an 
index within bounds for the list, yields a particular series 
ID number, which is what's wanted as an argument for 

A series ID number can be used as such in many contexts in 
gretl (mostly commands rather than functions), but if you try 
to use it in creating a matrix, as in

matrix matX = {lstX[1]}

the interpretation of the "ID number" as simply a number 
trumps its interpretation as the index of a series -- 
otherwise it would be impossible to construct a plain 
numerical matrix as, say,

matrix m = {1,2,3}

So: you can (now) index into a list using listname[i], but 
this gives you an integer result, which will be interpreted as 
a series index only in certain contexts (which should all be 
identified as such in the command and function help, I 

Allin Cottrell

More information about the Gretl-users mailing list