Yesterday I wrote ~50 lines of Perl that solved a one-off problem. In among that was a group of simple regexes that I am sure could be combined or written better if only I knew how. So I'd like to learn more, if someone would be kind enough to go all Perlmonks on me or attempt a Socratic dialogue. The code I wrote is:

my $plsname = $curdir; $plsname =~ s/^\Q$startdir\E//i; $plsname =~ s/\//_/g; $plsname =~ s/^_?//; $plsname .= '.pls';

with (e.g.) $curdir = 'Y:\Music\Schubert\Lieder\Terfel', $startdir = 'Y:\Music' and wanting 'Schubert_Lieder_Terfel.pls'

I tried to combine the second and fourth lines of the snippet, but while I think I could have solved that on my own, I realised that I was going to need multiple regexes and idleness set in. I suspect I should need only one regex, but my thinking processes are flawed.

What the rest of the code is doing is using File::Find to recurse through every subdirectory in the start directory and write a playlist file to another location. I have some 6,700 files in 300 directories. I now have a playlist for every directory, which is what I wanted.

Regards,

John Davies


In reply to Combining regexes by davies

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.