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)

Jesus! Thanks! Especially with the newline chomp thing. That was pretty
retarded but I would have never noticed. ;)

I really didn't need a count function there at all (as I learned from
searching the web today that $flac_count = $#music_list). I keep trying to
implement the fisher_yates_shuffle and I keep getting the error 

"Can't use string "/mnt/phoenix/sound/high_quality/" as an ARRAY ref while
"strict refs" in use at projects/flacme line 101

so I take use strict out and get ...

Modification of a non-creatable array value attempted, subscript -1 at
projects/flacme line 104 

Welp, this time I'll c&p the script in here

####Begin Script ;)

#!/usr/bin/perl

###############################################################
# Begin Modules
###############################################################

use warnings;
use strict; # We must for stability
use File::Find; # Needed for searching / portability
use Getopt::Std; # For shell switches

###############################################################
# Begin Global Variables
###############################################################

my @music_list = ();
my @root = qw "/mnt/phoenix/sound/high_quality/albums
/mnt/phoenix/sound/high_quality/individuals";

my %opt;

my $flac = "flac -s -c -d"; #temporary until .flacme is implemented
my $ver = "0.0.0a";
my $flac_count = 0;

###############################################################
# Begin Signal Handlers
###############################################################

$SIG{'INT'} = \&terminate;

###############################################################
# Begin Body of the Script
###############################################################

&usage();
&find_flac();
print `clear`;
&version();
&shuffle(@music_list);
&play();
&terminate();

###############################################################
# Begin Subroutines
###############################################################

# subroutine terminate: built for expandability :)
sub terminate { exit(0); }

# basic version information
sub version {
    print "\n\tflacme is copyrighted under the GNU General Public License";
    print "\n\tfor more information on this license read http://www.gnu.org/
";
    print "\n\tflacme-$ver\n\n\n"; 
}

# Purely evil and ugly find code (kept to the minimum for reading purposes)
sub find_flac { find (\&wanted, @root); }

sub wanted { 
    if ( /\.(flac)$/ ) { push (@music_list, "$File::Find::dir/$_") } 
}

sub random { my $music_phile = $_[rand @_];
             $flac_count = $#music_list;
         }

sub play 
{
    for (;;) 
    {
        my $music_phile = shift(@music_list);
        # $my $music_phile = &shuffle(@music_list);
        print "$flac_count playing randomly...\n\n$music_phile";
        system "$flac \"$music_phile\" | rawplay";
        print `clear`;
    } 
}

sub usage
{
    getopts('hv', \%opt);    
    if ($opt{v}) { &version; &terminate; &showopts; };
    if ($opt{h}) { &showopts };
}

sub showopts
{ 
    print "\nUSAGE: flacme [-vh]\n\n";
    print "OPTIONS:\n\n";
    print " -h\t\t\t\tthis help screen\n";
    print " -v\t\t\t\tprint version\n";
    &version;
    &terminate;
}

sub shuffle {
    my $music_shuffle = shift;
    my $i;

    for ( $i = @$music_shuffle; --$i; ) {
        my $j = int rand ($i+1);
        @$music_shuffle[$i,$j] = @$music_shuffle[$j,$i];
    }
}

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

Reply via email to