[Templates] Lists and rand like
http://faq.perl.org/perlfaq4.html#How_do_I_select_a_ra
Gavin Henry
ghenry@perl.me.uk
Tue, 18 Jul 2006 19:54:12 +0100 (BST)
quote who="Gavin Henry">
> 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 ;-)
I chose Philips one in the end. Thanks all:
[% USE Math %]
[% tips = ['test','test1','test2','test3'] %]
[% tips.${Math.rand(tips.size)} %]
Remind me, do the ${} force scalar context?
Thanks.
>
> Cheers,
>
> Gavin.
>
> --
> Walking the road to enlightenment... I found a penguin and a camel on the
> way..... Fancy a yourname@perl.me.uk? Just ask!!!
> http://perlmonks.org/?node_id=386673
>
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://lists.template-toolkit.org/mailman/listinfo/templates
>