Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am creating a random picture display script. the user defines how many pictures to be displayed on each page. But it also needs to be random. I am running into trouble with the random part. I can randomly select a picture from the directory and print it, but I need to then be able to remove that picture from the list to be displayed so that it will not be displayed again. here is the code i have so far:
for($i=1; $i<=$a; $i++){ $randomelement = $thumbs[rand @thumbs]; print "<img src=\"$picture_directory/$randomelement\">$picture_dir +ectory/$randomelement<br>"; splice(@thumbs, $randomelement,1); }
As you can see I am trying to use the splice function,but because it is random, it doesn't work. How do I remove $randomelement from @thumbs? @thumbs is the list of all the pictures in the directory.

Replies are listed 'Best First'.
Re: remove a random variable from an array
by Masem (Monsignor) on Apr 19, 2001 at 21:38 UTC
    Why not simply save the random number from rand @thumbs, and use that in the splice?
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
(ar0n) Re: remove a random variable from an array
by ar0n (Priest) on Apr 19, 2001 at 21:39 UTC
    Simple, assign the random index to a variable and splice using that variable:
    for (1..$a) { my $foo = rand @thumbs; my $randomelement = $thumbs[$foo]; my $url = "picture_directory/$randomelement"; print qq(<img src="$url">$url<br>); splice(@thumbs, $foo, 1); }

    Update: hooray for redundancy!

    ar0n ]

Re: remove a random variable from an array
by suaveant (Parson) on Apr 19, 2001 at 21:41 UTC
    do this
    for($i=1; $i<=$a; $i++){ $randomelement = int(rand @thumbs); print "<img src=\"$picture_directory/$thumbs[$randomelement]\"> +$picture_directory/$thumbs[$randomelement]<br>"; splice(@thumbs, $randomelement,1); }
    you need the int() since rand returns a decimal, and you were trying to splice out the value of the array index, not the actual index number...
                    - Ant

      @arr = qw(zero one two three four five); print "$_: $arr[$_]\n" for(3, 3.1415926);

      Prints:

      3: three 3.1415926: three

      No worries. Just thought you might like to know. :-)

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

        Huh... no real surprise perl is smarter than a decimal number :)
                        - Ant
Re: remove a random variable from an array
by Beatnik (Parson) on Apr 19, 2001 at 21:48 UTC
    $picture = splice(@images,int rand($#images)+1,1);
    Update:added +1... thx to ar0n

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      The only problem with this is, is that rand($#images) will never return the index of the last element in @images, since rand EXPR always returns less than EXPR.


      ar0n ]

      rand(@images) would also do what you want, without the math...
                      - Ant