On Tuesday 18 July 2006 11:02 am, Gavin Henry wrote:
> Dear all,
>
> 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:
>
> [% USE Math %]
>
> [% tips = [ 'test'
>
>               'test1'
>
>               'test2'
>
>               'test3' ] %]
>
> [% Math.rand(tips) %]
>
> 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.
>
> What's the proper way ;-)

In TT2 or TT3 or CGI::Ex::Template that would be something like:

[% i = Math.rand(tips.size) div 1 %]
[% tips.i %]

In CGI::Ex::Template or TT3 that would be either of the following:

[% tips.${ tips.size.rand } %]

OR 

[% tips.random %]

Paul Seamons

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to