On Mon, 7 Oct 2002, Fogle Cpl Shawn B wrote:

> I have studied
> the  fisher_yates_shuffle but I can't seem to implement it to save my life.
> It would help quite a bit if I understood what "@$" is and how to
> effectively use it. I don't see it in the beginners manual (and haven't
> searched the perldocs yet). Please feel free to diff me any improvements you
> have, I'd love to see what improvements can be made (be it known I am
> working on the short getopts).
> 
>       Thanks in advance
>       shawn

I will post the parts of your code that can be changed along with my 
comments.

$SIG{'INT'} = 'terminate';
# This is better written as $SIG{'INT'} = \&terminate;
# perldoc perlipc, look at the first page

sub find_flac { foreach (@root) { find ( \&wanted, "$_" ); } }
# You don't need the foreach here, the second argument to find is a list
# this can be rewritten as find (\&wanted, @root)
# perldoc File::Find

sub wanted {
    if ( /\.(flac)$/ ) { push (@music_list, "$File::Find::dir/$_\n") }
}
# You are pushing a string with a newline only to chomp it later on, why so?
# $File::Find::name contains the complete pathname to the file, you don't need
# a $File::Find::dir/$_

sub random { my $music_phile = $_[rand @_]; }
# The @$ is used to get to the array from the array reference that is
# passed into the sub. I will explain the code as given in
# perldoc -q shuffle

 sub fisher_yates_shuffle {
     my $deck = shift;  # $deck is a reference to an array
     my $i = @$deck; # @$deck gets to the array from the reference
                     # In scalar context this will give the number
                     # of elements in the array.
     while ($i--) {
         my $j = int rand ($i+1);
         # perldoc -f rand, rand returns a value greater than or equal to 0
         # and less than it's argument. In this case, greater than or equal to 0
         # and less than the current index + 1. This makes sure that the indices
         # that have been shuffled do not get shuffled again

         @$deck[$i,$j] = @$deck[$j,$i];
         # This is how you swap array elements in perl. Swap the value of the
         # current index with the one returned from rand.
         # This style (@$deck[$i,$j]) is called an array slice
     }
}

# You also have a choice to use the shuffle function from List::Util.
# You can get this module from 
# http://search.cpan.org/author/GBARR/Scalar-List-Utils-1.0701/
# You can shuffle your array by including this function and calling
# it as fisher_yates_shuffle (\@music_list)

sub count { foreach (@music_list) { $flac_count++; }; }
# @music_list used in a scalar context will give you the no of elements in that array.
# You don't need the foreach loop here just a $flac_count = @music_list should do




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to