At 05:44 PM 7/31/2001, you wrote:
>On Tue, Jul 31, 2001 at 06:32:15PM -0500, CDitty wrote:
>: In both my perl books. Learning Perl(O'Reilly) and Perl 5 Interactive
>: Course(Waite Group).
>:
>: The shuffle routine in not listed in either of these books, although they
>: are almost 1 year old. When I try and use shuffle I get
>: Undefined subroutine &main::shuffle and my program stops.
>
>Did you read the documentation provided by executing 'perldoc -q
>shuffle'?
Yes, but since I am just learning perl, the example is greek to me and
means nothing.
I also do not that access to my telnet right now.
http://www.perldoc.com/cpan/Algorithm/Numerical/Shuffle.html#toc
>If you did, you noticed a subroutine in the coding example that
>shuffles an array with the fisher-yates algorithm. I'll post it here
>so everyone can see it
Not my copy.
use Algorithm::Numerical::Shuffle
qw /shuffle/;
@shuffled = shuffle (1, 2, 3, 4, 5, 6, 7);
$in_situ = [qw /one two three four five six/];
shuffle $in_situ;
Even adding the first line above causes a server error.
> Use this:
>
> # fisher_yates_shuffle( \@array ) :
> # generate a random permutation of @array in place
> sub fisher_yates_shuffle {
> my $array = shift;
> my $i;
> for ($i = @$array; --$i; ) {
> my $j = int rand ($i+1);
> next if $i == $j;
> @$array[$i,$j] = @$array[$j,$i];
> }
> }
>
> fisher_yates_shuffle( \@array ); # permutes @array in place
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]