On Tue, Jul 31, 2001 at 06:05:18PM -0500, CDitty wrote:
> Hello all.  Just joined the list and after reading through the archives, I 
> couldn't find an answer.  I'm hoping someone can help.
> 
> I have an array with 6 variables in it.   I need to randomize the variables 
> in this array.  According to the documentation I have read, 
> rand(@display_pictures) should do it.    But it doesn't.  Everytime I 
> output the array, it is in the same order it was put in.

I'm not sure what documentation told you this, but if it truly did, it's
wrong.  It's possible you misunderstood it.

rand(@display_pictures) returns a random number (notice: number, not
integer) greater than or equal to 0, and less than the length of
@display_pictures.  This can be used as an index into the array.  If you
want a randomized array based on what's in @display_pictures:

    my @rand_display_pictures;
    while (@display_pictures) {
        push(
            @rand_display_pictures,
            splice(@display_pictures, rand(@display_pictures), 1)
        );
    }

@rand_display_pictures is a randomized version of @display_pictures.  This
operation is destructive, meaning @display_pictures will be empty when it's
done.


If, on the other hand, you just want a random element from
@display_pictures, simply access it:

    $display_pictures[rand @display_pictures];


See perldoc -f rand.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to