Hi Allin,
many thanks and sry for my late help. I'll save that info.
Best
Frederik
-----Ursprüngliche Nachricht-----
Von: gretl-users-bounces(a)lists.wfu.edu [mailto:gretl-users-
bounces(a)lists.wfu.edu] Im Auftrag von Allin Cottrell
Gesendet: Freitag, 15. Dezember 2017 20:40
An: Gretl list <gretl-users(a)lists.wfu.edu>
Betreff: Re: [Gretl-users] foreach vs for with list
On Fri, 15 Dec 2017, Allin Cottrell wrote:
> On Fri, 15 Dec 2017, Schaff, Frederik wrote:
>
>> Hi there, I'd expect both options to be equivalent (add a matrix with
>> the data from the series to the array of matrices). However, the
>> forach loop only adds the first data-point to the matrix.
>>
>> <hansl>
>> open denmark.gdt -q
>> list ls = 1 4
>> matrices M = null
>> loop foreach l ls
>> matrix temp = l
>> colnames(temp,varname(l))
>> M+=temp
>> endloop
>>
>> matrices M2 = null
>> loop s=1..nelem(ls)
>> matrix temp = ls[s]
>> colnames(temp,varname(ls[s]))
>> M2 += temp
>> endloop
>
> In your first loop, 'l' is a just scalar index, so
>
> matrix temp = l
>
> gives you {1} on the first iteration and {2} on the second. If you
> want to access the series at position l in the list you need to say
>
> matrix temp = $l
>
> Then you'll get the same output as from your second loop.
P.S. the varname() call in your first loop also needs fixing: on the second
iteration l = 2 but presumably you want the name of the second series in the
list, namely series 4, so:
colnames(temp,varname(ls[l]))
Allin
_______________________________________________
Gretl-users mailing list
Gretl-users(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-users