[EMAIL PROTECTED] wrote:
> Hello,
Hello,
> I am working on a playlist managment program in perl - I need a way to
> shuffle the playlist - the playlist format is very simple - one
> filename on each line.
>
> Each file name is a reletive path, i.e:
>
> folder1/file1.mp3
> folder2/file2.mp3
>
> If possible I would like the file to be shuffled so that whenever
> possible, there are no 2 files from the same folder next to each
> other.
perldoc -q shuffle
> In the origional file, pre-shuffling, the files are sorted into folders, i.e:
>
> folder1/file1.mp3
> folder1/file2.mp3
> folder2/file3.mp3
> ......
>
> It would be an adequate solution to just cycle through all the folders, like:
>
> folder1/file1.mp3
> folder2/file1.mp3
> folder3/file1.mp3
> folder1/file2.mp3
> .......
This may do what you want:
my %folders;
# assuming that @playlist contains folder/file names
for ( @playlist ) {
my ( $folder ) = m!^(.+)/!;
push @{ $folders{ $folder } }, $_;
}
# use the shuffle sub from perlfaq4
fisher_yates_shuffle( $folders{ $_ } ) for keys %folders;
while ( %folders ) {
my @curr;
for ( keys %folders ) {
push @curr, pop @{ $folders{ $_ } };
delete $folders{ $_ } unless @{ $folders{ $_ } };
}
print "$_\n" for @curr;
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>