[Templates] Lists and rand like http://faq.perl.org/perlfaq4.html#How_do_I_select_a_ra
Todd Freeman
freeman@andrews.edu
Tue, 18 Jul 2006 13:18:32 -0400
--E39vaYmALEf/7YXx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
I just made my own rand vmethod (yes it's ugly... but I do take
suggestions :} )
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
package Template::VMethod::Rand;
use base qw(Template::Plugin);
use strict;
sub load {
my ( $class, $context ) =3D @_;
# Set this twice to avoid a "used only once" warning
$Template::Stash::LIST_OPS->{rand} =3D \&randomizer;
$Template::Stash::LIST_OPS->{rand} =3D \&randomizer;
=09
return $class;
}
sub randomizer
{ return $_[0][int(rand($#{$_[0]}+1))]; }
1;
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
On Tue, Jul 18, 2006 at 06:02:17PM +0100, 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'
>=20
> 'test1'
>=20
> 'test2'
>=20
> '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 ;-)
>=20
> Cheers,
>=20
> Gavin.
>=20
> --=20
> 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=3D386673
>=20
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://lists.template-toolkit.org/mailman/listinfo/templates
--=20
Todd Freeman Ext 6103 .^. Don't fear the penguins!
Programming Department /V\
Andrews University // \\ http://www.linux.org/
http://www.andrews.edu/~freeman/ /( )\ http://www.debian.org/
^^ ^^
--E39vaYmALEf/7YXx
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFEvRfonbl1fXtXLfERAv4jAJ9OtViGdnAtwrlM2MwQjtQXGlt+awCfRttz
yIh379PY4O6BwG7DtJ4R7ZI=
=BZca
-----END PGP SIGNATURE-----
--E39vaYmALEf/7YXx--