[Omaha.pm] quick n dirty rand()

Andy Lester andy at petdance.com
Thu Jun 9 18:50:03 PDT 2005


On Thu, Jun 09, 2005 at 06:04:16PM -0500, Jay Hannah (jhannah at omnihotels.com) wrote:
> 
> Before
> 
>   my $rand = int(rand(3))+1;
>   my $imagename = "sean.jpg";
>   $imagename = "jay.jpg"      if ($rand == 2);
>   $imagename = "airplane.jpg" if ($rand == 3);
> 
> After
> 
>   my $imagename = ("sean.jpg", "jay.jpg", "airplane.jpg")[(int rand 3)];

Better yet:

my @images = qw( sean.jpg jay.jpg airplane.jpg );
my $random_image = $images[rand @images];

-- 
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance


More information about the Omaha-pm mailing list