Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: unsorted list

by dbwiz (Curate)
on Apr 24, 2005 at 07:04 UTC ( #450892=note: print w/ replies, xml ) Need Help??


in reply to unsorted list

You can use rand within the sort code.

@stuff = qw[ cat dog pig cow ]; print "$_\n" for sort { rand() <=> rand() } @stuff

Update. I have the (theoretical) feeling that this trick may cause an infinite loop when used with a large array, but a few tests I did returned nothing suspicious.

HTH


Comment on Re: unsorted list
Download Code
Replies are listed 'Best First'.
Re^2: unsorted list
by merlyn (Sage) on Apr 24, 2005 at 13:32 UTC
    In older Perl versions, inconsistent results from the sort block could produce core dumps or duplicated values. I don't believe modern Perl ever does that, but it's still not a way to get a fair sort.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re^2: unsorted list
by Roy Johnson (Monsignor) on Apr 24, 2005 at 12:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://450892]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-07-28 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls