In article <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]> wrote:
> I'd prefer to just fix the error in fy1, and put
>
> sub fy1 {
> my $deck = shift; # $deck is a reference to an array
> my $i = @$deck;
> while ($i--) {
> my $j = int rand ($i+1);
> @$deck[$i,$j] = @$deck[$j,$i];
> }
> }
and here's the proposed patch for comments:
localhost_brian[3004]$ cvs diff -u -d perlfaq4.pod
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.19
diff -u -d -r1.19 perlfaq4.pod
--- perlfaq4.pod 11 Mar 2002 22:15:19 -0000 1.19
+++ perlfaq4.pod 4 Apr 2002 17:59:34 -0000
@@ -1335,28 +1335,21 @@
If you either have Perl 5.8.0 or later installed, or if you have
Scalar-List-Utils 1.03 or later installed, you can say:
- use List::Util 'shuffle';
+ use List::Util 'shuffle';
@shuffled = shuffle(@list);
-If not, you can use this:
+If not, you can use a Fisher-Yates shuffle.
- # fisher_yates_shuffle
- # generate a random permutation of an array in place
- # As in shuffling a deck of cards
- #
sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
- while (--$i) {
+ while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}
-And here is an example of using it:
-
- #
# shuffle my mpeg collection
#
my @mpeg = <audio/*/*.mp3>;