Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-06-17 12:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.