Hello,
I have simple script that generates a random list of 5 images from a specific
folder, but sometimes it lists the same image twice within the random array list
created. Is there a better way to generate a list so it will not display the
same element twice within the array??
Below is the code we are currently using:
############# SNIP ###########################################
my @randomstyles = ();
my $maxstyles = 5;
my @styles = ();
my $images = 0;
srand(time());
# READ ALL IMAGES FROM SAMPLE FOLDER
opendir(DIR,$conf{samplestyples}) || die "Can't open $conf{samplestyples} folder
$!";
my @samplestyles = readdir(DIR);
closedir(DIR);
LOOP:
for my $style (@samplestyles)
{
# Skip non-image
next LOOP unless $style =~ /\.gif$/;
push(@randomstyles,$style);
$images++;
}
# GENERATE RANDOM ARRAY LIST OF ALL IMAGES
for (1..$images) { push(@styles, $randomstyles[rand @randomstyles]); }
print $cgi->header();
# DISPLAY 5 RANDOM IMAGES FROM ARRAY LIST
for (my $i=0; $i<@styles; $i++)
{
chomp $styles[$i];
print qq~Style: $styles[$i]<BR>~;
last if ($i+1) == $maxstyles;
}
##################################################################
TIA,
Mike(mickalo)Blezien
===============================
Thunder Rain Internet Publishing
Providing Internet Solution that Work
http://www.thunder-rain.com
===============================
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>