[Templates] Using FOREACH i IN [0 .. max]

Mark Anthony manthony1@iname.com
Fri, 27 Apr 2007 15:49:42 -0500


This is a multi-part message in MIME format.

--_----------=_1177706983325881
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="iso-8859-1"

Hi,

I have a sort of newbie question on using FOREACH with a range. I have a
collection of variables passed to Template Toolkit, and it'd be nice if I
can use LISTSIZE-1 kind of thing with FOREACH. In Perl, I would write
something like this:

for ($i=3D0; $i < $LISTSIZE; $i++) {
# Doing stuff with $i
}

I couldn't find a "for" inside TT, but I saw that I could write something
like:

[% FOREACH i IN [0 .. LISTSIZE] %]
... Doing stuff with [% i %]
[% END %]

This almost does what I want, but it includes LISTSIZE. I just need 0 to
LISTSIZE-1. So I've tried the following:

[% FOREACH i IN [0 .. LISTSIZE-1] %]
[% FOREACH i IN [0 .. LISTSIZE - 1 ] %]
[% FOREACH i IN [0 .. (LISTSIZE-1) ] %]
[% FOREACH i IN [0 .. (LISTSIZE - 1) ] %]
[% FOREACH i IN [0 .. "$LISTSIZE"-1 ] %]

But TT either doesn't like the minus sign or the open parens. I know I
can write something like:

[% SET max =3D LISTSIZE - 1; FOREACH i IN [0 .. max] %]
... Doing stuff with [% i %]
[% END %]

Or:

[% FOREACH j IN [1 .. LISTSIZE] %]
[% SET i =3D j - 1 %]
... Doing stuff with [% i %]
[% END %]

But that seems more error-prone. It'd be nice to be able to use
LISTSIZE-1 inside the range-specifier, but I'm having no luck figuring
out the "trick".

Any help would be appreciated. I'm using Template Toolkit 2.14. Oh, I
also tried out the syntax in Perl:

perl -e '$max=3D10; foreach $i (0 .. $max-1){ print "$i\n" }'

And it prints from 0 up to 9, so it doesn't look like Perl inherently has
this limitation.

Thanks
Mark

--_----------=_1177706983325881
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="iso-8859-1"

Hi,<br><br>I have a sort of newbie question on using FOREACH with a range. =
I have a collection of variables passed to Template Toolkit, and it'd be ni=
ce if I can use LISTSIZE-1 kind of thing with FOREACH. In Perl, I would wri=
te something like this:<br><br>for ($i=3D0; $i &lt; $LISTSIZE; $i++) {<br>&=
nbsp;&nbsp; # Doing stuff with $i<br>}<br><br>I couldn't find a "for" insid=
e TT, but I saw that I could write something like:<br><br>[% FOREACH i IN [=
0 .. LISTSIZE] %]<br>&nbsp;&nbsp; ... Doing stuff with [% i %]<br>[% END %]=
<br><br>This almost does what I want, but it includes LISTSIZE. I just need=
 0 to LISTSIZE-1. So I've tried the following:<br><br>[% FOREACH i IN [0 ..=
 LISTSIZE-1] %]<br>
[% FOREACH i IN [0 .. LISTSIZE - 1 ] %]<br>
[% FOREACH i IN [0 .. (LISTSIZE-1) ] %]<br>[% FOREACH i IN [0 .. (LISTSIZE =
- 1) ] %]<br>[% FOREACH i IN [0 .. "$LISTSIZE"-1 ] %]<br><br>But TT either =
doesn't like the minus sign or the open parens. I know I can write somethin=
g like:<br><br>[% SET max =3D LISTSIZE - 1; FOREACH i IN [0 .. max] %]<br>
&nbsp;&nbsp; ... Doing stuff with [% i %]<br>
[% END %]<br>
<br>
Or:<br><br>[% FOREACH j IN [1 .. LISTSIZE] %]<br>&nbsp;&nbsp; [% SET i =3D =
j - 1 %]<br>
&nbsp;&nbsp; ... Doing stuff with [% i %]<br>
[% END %]<br>
<br>
But that seems more error-prone. It'd be nice to be able to use LISTSIZE-1 =
inside the range-specifier, but I'm having no luck figuring out the "trick"=
.<br><br>Any help would be appreciated. I'm using Template Toolkit 2.14. Oh=
, I also tried out the syntax in Perl:<br><br>perl -e '$max=3D10; foreach $=
i (0 .. $max-1){ print "$i\n" }'<br><br>And it prints from 0 up to 9, so it=
 doesn't look like Perl inherently has this limitation.<br><br>Thanks<br>Ma=
rk<br><br>
<div>

</div>
<BR>

--=20
<p>
<a href=3D"http://a8-asy.a8ww.net/a8-ads/adftrclick?redirectid=3Den-mail_a_=
01" target=3D_blank>
<img alt=3D"" src=3D"http://a8-asy.a8ww.net/a8-ads/adftrview?redirectid=3De=
n-mail_i_01" border=3D"0"></a>
</p>

--_----------=_1177706983325881--