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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difficulty randomizing 1

Status
Not open for further replies.

ke5jli

Technical User
Jan 1, 2007
13
0
0
US
I apologize for this post as I can see that the question had been adressed several times in the past however, I am having difficulty applying the examples to my code. I am using a purchased database script and I need to randomize the output. The script currently uses
Code:
@keepers=sort(@keepers);
for standard sorting and
Code:
@keepers=reverse(sort(@keepers));
for reverse sorting. I need random sorting. Can anyone please help using the above code as a guide?

Thank You!
 
Just did a quick Google search and found these two codes:

Code:
    use List::Util 'shuffle';

	@shuffled = shuffle(@list);

Code:
    sub fisher_yates_shuffle {
        my $deck = shift;  # $deck is a reference to an array
        my $i = @$deck;
        while ($i--) {
            my $j = int rand ($i+1);
            @$deck[$i,$j] = @$deck[$j,$i];
        }
    }

    # shuffle my mpeg collection
    #
    my @mpeg = <audio/*/*.mp3>;
    fisher_yates_shuffle( \@mpeg );    # randomize @mpeg in place
    print @mpeg;

-------------
Cuvou.com | The NEW Kirsle.net
 
I would go with the List::Util module as Kirsle shows above, it's a core module so you should be able to just plug those lines into your existing code and get what you want.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top