[Templates] Lists and rand like http://faq.perl.org/perlfaq4.html#How_do_I_select_a_ra

Garrett, Philip (MAN-Corporate) Philip.Garrett@manheim.com
Tue, 18 Jul 2006 13:19:04 -0400


Gavin Henry wrote:
> Dear all,
>=20
> I am trying to get a random image/"Tip of the Day" from a list of
> images/tips for one of our sites. I was playing naively with:
>=20
> [% USE Math %]
>=20
> [% tips =3D [ 'test'
>               'test1'
>               'test2'
>               'test3' ] %]
>=20
> [% Math.rand(tips) %]
>=20
> But in my eyes tips is an array ref, so it will be using the memory
> address of that array and giving me a random fractional number greater
> than or equal to 0 and less than the value of this address.
>=20
> What's the proper way ;-)

My version of TT2 doesn't have the Math plugin, but I think this should
work:

[% USE Math %]
[% tips =3D ['test','test1','test2','test3'] %]
[% tips.${Math.rand(tips.size)} %]

Philip