[Gretl-users] Access elements of a list

Logan Kelly logan.kelly at uwrf.edu
Thu Jul 25 19:20:57 EDT 2013


Thanks Allin,

I see your point, but 

Example A:

list lstX = 1 2 3 4
matrix matX = {lstX}
 
is different from 

Example B:

matrix matX = {1,2,3,4}

and

matrix matX = {lstX[i]}

seem much more like Example A. But I definitely  see your point. I was just surprised.  It is just as easy to store the list in a matrix and access each column of the matrix.

 Cheers and thanks,

Logan

-----Original Message-----
From: gretl-users-bounces at lists.wfu.edu [mailto:gretl-users-bounces at lists.wfu.edu] On Behalf Of Allin Cottrell
Sent: Thursday, July 25, 2013 5:26 PM
To: Gretl list
Subject: Re: [Gretl-users] Access elements of a list

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 varname().

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 believe).

Allin Cottrell




_______________________________________________
Gretl-users mailing list
Gretl-users at lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-users



More information about the Gretl-users mailing list