in reply to Opening random files (with bias) based on File::Stat information.

I don't know about access times in Windows -- you may have to keep track separately of when you've played the audio files if other things are interfering with the access times.

As for the general structure of your code, you are right -- all those evals are atrocious. Instantiating global variables via eval as a way to implement argument passing? @file0 through @file8?? Ew.

And for prior art in the subject, this node is a good starting place for picking a weighted random number..

sub score { ## however you decide to assign a score to a filename } sub choose_weighted { ## from that other node } my @audio_files; find( sub { /\.(mp3|wav|wma)$/ && push @audio_files, $File::Find::name } +, 'C:\music' ); ## this could go inside the while loop if you needed to recalculate ## relative weights after every song is played: my @weights = map { score($_) } @audio_files; while (1) { my $index = choose_weighted(\@weights); system 'c:\Program Files\Windows Media Player\wmplayer.exe', $audio_files[$index]; print "Another? "; last unless <STDIN> =~ /^y/i }
Take a look at that system line. It is about a million times safer (and easier on the eyes) than eval'ing a backtick command. With multi-arg system, you don't have to worry about quoting issues either.

Update: loaded @audio_files from File::Find

blokhead

  • Comment on Re: Opening random files (with bias) based on File::Stat information.
  • Download Code