mumble jumbling.
Stray Toaster
mwk at stray-toaster.co.uk
Thu Jan 3 04:01:30 CST 2002
On Thu, Jan 03, 2002 at 09:58:55AM +0000, Leon Brocard wrote:
> Stray Toaster sent the following bits through the ether:
>
> > I want to take an array, and then make another out of it, albeit a
> > jumbled up version. Now there is bound to be a nice way of doing this.
>
> 'perldoc -q shuffle' introduces you to the nice Fisher Yates Shuffle,
> which in your code would be equal to:
>
> my $fisher_yates_shuffle = sub {
> my $array = [@_];
> my $i;
> for ($i = @$array; --$i; ) {
> my $j = int rand ($i+1);
> @$array[$i,$j] = @$array[$j,$i];
> }
> return @$array;
> };
wow, ta. And reading the perldoc, I never realised the overhead that
splicing would have. tcah.
I thank you!
m.
--
So they went off together. But wherever
they go, and whatever happens to them on the way,
in that enchanted place on the top of the Forest
a little boy and his Bear will always be playing.
More information about the Belfast-pm
mailing list