On Fri, Apr 05, 2002 at 12:17:27AM +0300, Ilmari Karonen wrote:
>
> > I must say however, that requiring that the array is passed as a
> > reference seems an oddity to me. If you pass it a list, it should,
> > IMO, return the shuffled list.
>
> The point of taking a reference is that the Fisher-Yates algorithm is an
> in-place shuffle. If your array happens to be a couple of megabytes in
> size, you start to appreciate this feature. So, since we have this nice
> in-place shuffle algorithm, which is just as simple and effective as any
> non-in-place alternative could ever be, it seems silly to start making
> copies of arrays just to satisfy Perl's return-by-value semantics.
Ah, well, one could give the same argument for sort and map, but
noone seems to object to them not performing in situ operations.
The advantages of taking a list as argument are there too:
my @shuffled = shuffle 1 .. 10;
reads better than:
shuffle \my @shuffled = 1 .. 10;
or
my @shuffled = 1 .. 10;
shuffle \@shuffled;
> In any case, if you want to shuffle a copy of an array, you can -- just
> make the copy before shuffling it. Sure, it's a little extra work, but
> it's no worse than what you have to do with s///. Turning an in-place
> shuffle into copy-and-shuffle is easy, the other way is impossible.
Impossible? I wonder how people manage to sort arrays then....
@array = shuffle @array; # Tada!
Abigail