foreach (0 .. $#array) { my $i = rand @arr; next if $i == $_; @array[$_, $i] = @array[$i, $_]; }