[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] quick n dirty rand()




On Jun 09, 2005, at 20:50, Andy Lester wrote:
On Thu, Jun 09, 2005 at 06:04:16PM -0500, Jay Hannah (jhannah@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];

Thanks! We ended up putting this "in production":

my $imagename = ("sean.jpg", "jay.jpg", "airplane.jpg")[(int rand 3)];

Grin,

j