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.
As always, please define "as expected". And what are you
getting instead?
<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
?M
?M2
</hansl>
produces:
<output>
? M
Array of matrices, length 2
[1] 1 x 1
[2] 1 x 1
? M2
Array of matrices, length 2
[1] 55 x 1
[2] 55 x 1
<\output>
> -----Ursprüngliche Nachricht-----
> Von: gretl-users-bounces(a)lists.wfu.edu [mailto:gretl-users-
> bounces(a)lists.wfu.edu] Im Auftrag von Sven Schreiber
> Gesendet: Freitag, 15. Dezember 2017 17:55
> An: gretl-users(a)lists.wfu.edu
> Betreff: Re: [Gretl-users] foreach vs for with list
>
> Am 15.12.2017 um 17:49 schrieb Schaff, Frederik:
>
> > why are those not equivalent? I.e., the foreach (which is more
> > concise) does not work as (I) expected.
>
As always, please define "as expected". And what are you
getting instead?
>
> > loop for s=1..nelem(ls)
>
> BTW, this syntax is not optimal (even though it's tolerated) -- for the simplest
> index loop omit the "for": loop s=1..nelem(ls) will do.
>
> cheers,
> sven
> _______________________________________________
> Gretl-users mailing list
> Gretl-users(a)lists.wfu.edu
>
http://lists.wfu.edu/mailman/listinfo/gretl-users