in reply to Easier way to do this

Look into File::Find. One of the first things to learn with perl is how to use already existing modules and libraries... no need to reinvent the wheel on this one.

Something like this is what you'd end up with:
use strict; use File::Find; use File::Basename; my($topdir) = '/'; # where you want to start from my(@files) = find ( \&wanted, "$topdir" ); foreach (@files) { my($dir) = dirname($_); if ($dir ne $prev_dir) { print "<P>" . $dir . "</P>\n"; $prev_dir = $dir; } print "<BLOCKQUOTE>" . $_ . "</BLOCKQUOTE>\n"; } sub wanted { /\.mp3$/ && -f; }
Hope that helps,
Shendal

Update: As Corion so aptly put, my regex was simply wrong. I fixed it. Serves me right for posting untested code.

Replies are listed 'Best First'.
RE: Re: Easier way to do this
by Anonymous Monk on Aug 18, 2000 at 06:47 UTC

    Your reply is the easiest for me to understand, but there seems to be an error in the regexp. I get this response.

    
    Thu Aug 17 21:41:17 2000 index.cgi: /*.mp3$/: ?+*{} follows nothing in regexp at ./index.cgi line 34.
    
    
    So what happenened?? I am no good at regexps, and I've never used them out of m// or s///. I have copied your code exactly three times, I tried to make sense of it, but it has me stumped (surprise, surprise, surprise).

      The regular expression is simply wrong :-). A regular expression that matches everything with the extension .mp3 would be for example :

      /\.mp3$/

      A quick explanation : The \.mp3 part is to ask for a literal dot followed by mp3 and the $ part is that the string must end after this. This is also the shortest regular expression that will match all files with an extension of .mp3.

      Another regular expression, more in the spirit of the faulty RE would have been

      /.*\.mp3$/
      in which you'll recognize the \.mp3$ part from above plus the .* part, which means "match an arbitrary character" (the dot) "zero or more times" (the star).

      As a Perl newbie, getting familiar with regular expressions is quite a task, but you can never start too early with them - in fact, I like Perl as it is the only language I know with REs built into the language core :-)

      Some recommended reading on regular expressions is the RE man page and of course the book "Mastering Regular Expressions" by Jeffrey Friedel - but that one contains more about REs than you ever thought there is to know.