in reply to randomize array

If you are expecting the statement
@$array[$i,$j] = @$array[$j,$i];
to interchange the elements, you are missing a step or two. You need to do something like:
my $temp = $array->[$i,$j]; $array->[$i,$j] = $array->[$j,$i]; $array->[$j,$i] = $temp;
Update:I stand corrected - Thanks, Thelonius. Please Ignore this node. <Feeble_excuse>Late night brain block did not process the array-ref-slice syntax correctly</Feeble_excuse>.

     "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Replies are listed 'Best First'.
Re^2: randomize array
by Thelonius (Priest) on Mar 29, 2006 at 06:31 UTC
    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)];