Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with this random image script

Status
Not open for further replies.

MattUSA

MIS
Joined
Aug 20, 2006
Messages
3
Location
GB
Hi,

I'n new to the forum + to perl. I have this script below, which displays images at random within the table. What I'm looking is for a way to stop it from displaying the same image more than once - occasionally the same images are displayed within the table. Any advice would be great.

Thank You

#!/usr/bin/perl

#====================== Edit ============================

$basedir="/public_html/cgi-bin/";

@img = ("imag1.JPG",
"imag2.JPG",
"imag3.JPG",
"imag4.JPG",
"imag5.JPG",
"imag6.JPG",
"imag7.JPG",
"imag8.JPG",
"imag9.JPG",
);



#====================== Done ============================

srand($$ & time ^ $$);
$imgnum = rand(@img);

print "Content-type: text/html\n\n";

print "<table width=728px align=center><tr>";

for (my $images = 1; $images <= 4; $images++) {
$imgnum = rand(@img);
print "<td><img src=/";
print "@img[$imgnum]";
print "></a></td>";
}

print "</tr></table>";

$imgnum = "";
 
Try a hash.

Code:
print "<table width=728px align=center><tr>";

my %used = ();

for (my $images = 1; $images <= 4; $images++) {
        $imgnum = rand(@img);

        # skip if this image has been used already
        next if exists $used{$imgnum};

        $used{$imgnum} = "yes"; # create a hash key
           # so we won't reuse this image

        print "<td><img src=/";
        print "$img[$imgnum]"; # also, $img[$imgnum] is better syntax than @img[$imgnum]
        print "></a></td>";
}

print "</tr></table>";
 
Thanks Kirsle. It works a lot better than before, but occasionally still shows the same images. Cheers
 
diaplay once or not display twice in a row or display all images before repeating an image?
 
not display twice in a row ideally
 
See if this works OK. Make a file with all the images (images.txt) the last line should be a word that is not the same as any of the images, for example:

Code:
frog
cat
cow
bird
dog
fish
camel
pig
horse
skunk
snake
chicken
elephant
skunk
>><<


the script will overwrite the last line the first time it runs. The last line will be used to store the name of the last image displayed.

Code:
# put these three lines at beginning of script somewhere
use Tie::File;
use Fcntl;
my $path = 'path/to/images.txt';

tie my @images, 'Tie::File', $path, mode => O_RDWR or die "Can't open $path: $!";
print qq~<table width="728" align="center"><tr>\n~;
print qq~<td><img src="~, get_image(), qq~"></td>\n~ for (1..3);
print '</tr></table>';
untie(@images);

sub get_image {
   my $image = $images[int(rand(scalar@images-1))];
   if ($image eq $images[$#images]) {
      get_image()
   }
   else {
      $images[$#images] = $image;   
      return($image);
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top