Hi Monks, I have a problem. I would like to generate a random playlist for an old mxaudio program (SGI O2, perl version 5). My goal is to take the contents of a directory structure containing a very large number of audio files and generate a list of a given number (say 10 tracks) of unique mp3s. It is important that reading of the start folder is recursive and that it is possible to add exceptions. I attach the code as far as I have got. The unique list will add up, but if the random function selects the same file more than once, then of course with fewer items than I would like. Can you help me with this? Thanks Zsolt.
#!/usr/bin/perl my $target = "/home/zsolti/Temp"; my @exclude = ( #'2023_02_21_Szentendre_Pilis_EK_oldal' 'webftp' ,'x' ); my (@audioFiles, %playList); my $numOfRandFiles = 10; &fileExplore($target); #print join("\n", @audioFiles), "\n"; &uploadPlaylist; print "-" x 80, "\n", "SELECTED FILES:\n", "-" x 80, "\n"; foreach $key (sort {$playList{$a} <=> $playList{$b}} (keys(%playList)) +) { print "$playList{$key}: $key\n"; } sub fileExplore { my $dir = shift; my $hit = 0; local *DIR; opendir DIR, $dir or die "opendir $dir: $!"; my $found = 0; while ($_ = readdir DIR) { next if /^\.{1,2}$/; $FSNode = $_; my $FSObj = "$dir/$FSNode"; foreach my $exc (@exclude) {$hit = 1 if $FSObj =~ m/\/$exc\//g} next if $hit; if (-f $FSObj) { (my $fExt = $FSObj) =~ s/.*\.(.*$)/$1/i; push(@audioFiles, $FSObj) if lc($fExt) eq 'mp3'; } fileExplore($FSObj) if -d $FSObj; } closedir DIR; } sub uploadPlaylist { for (my $i = 1; $i <= $numOfRandFiles; $i++) { $playList{$audioFiles[int rand@audioFiles]} = $i; } }

In reply to Add a fixed number of unique elements to hash by Vasek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.