Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The code looks like this:
My 2 questions:for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i] }
1) in the for loop: I see a predecrement where a guard should be.
what's with that? I understand C-style for loops to be
for(init..; guard..; increment/decrement) {..}
2) $array is a reference to a 1-D array, yet the last line here uses indices appropriate for a 2-D array. Why does this work and how?
Nuggets of wisdom would be dearly appreciated.
|
|---|