in reply to Array Shuffle
You'll see that in the output, no element appears in its original position (after the first line, which is there for reference).use strict; use warnings; my @array = (1..9); sub derange { use List::Util 'shuffle'; my @list = shuffle @_; my %map = map {($list[$_-1] => $list[$_])} 0..$#list; print "@map{@_}\n"; } print "@array\n"; derange(@array) for 1..15;
One slightly non-obvious thing I did was to use negative indexing so that my mapping would wrap around from the last element to the first.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array Shuffle
by ambrus (Abbot) on Feb 28, 2006 at 21:54 UTC | |
by Roy Johnson (Monsignor) on Mar 01, 2006 at 15:41 UTC |