Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: randomize array

by Thelonius (Priest)
on Mar 29, 2006 at 06:31 UTC ( [id://539877]=note: print w/replies, xml ) Need Help??


in reply to Re: randomize array
in thread randomize array

If you are expecting the statement ... to interchange the elements, you are missing a step or two.
No, that part works fine:
my @usernames = qw(zero one two three four five six); testswap(\@usernames); print join(" ", @usernames), "\n"; sub testswap { my $array = shift; my $i = 2; my $j = 5; @$array[$i, $j] = @$array[$j, $i]; }
Prints: zero one five three four two six

As runrig pointed out, you're not returing a value, you could put return $array->[0] at the end of the subroutine.

If you just want to pick one element out of the list, you're going to too much trouble. You can just say:

my $username = $usernames[rand(@usernames)];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-18 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found