abigail wrote

>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.
>

This is not true,  I remember discussions about in-place-sort some weeks ago
in this list. Apart from that, I feel many people (rightly) just expect sort
to work in situ, and are puzzled to see sort @a in a void context not having
the desired effect.

>> 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!

This is not an in-place-shuffle. The array is copied, shuffled, and copied
back in that process. (Or am I missing some magic here?)

Joachim

-- 
there is only one way to do it

Reply via email to