Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl Golf - Renaming MP3s

by buzzcutbuddha (Chaplain)
on Nov 18, 2002 at 21:54 UTC ( [id://213944]=perlmeditation: print w/replies, xml ) Need Help??

This little Golf Challenge made it's way through the Harrisburg Perl Mongers mailing list today:

You have a bunch of MP3s in directory with sub-directories, and each have their track number prefixed as a two digit number followed by a dash and then the song title. So for example you'd have a directory music which had directories like The Who, Eminem, etc. and the files would look like 03-1921.mp3, 03-8 Mile.mp3.

If you wanted to find all the MP3s and remove the track number and dash, how would you do it in the most compact form?

Using OS tools like find and mv are okay, but extra bonus points to those who implement the shortest all Perl solution.

Replies are listed 'Best First'.
Re: Perl Golf - Renaming MP3s
by nothingmuch (Priest) on Nov 18, 2002 at 22:33 UTC
    use File::Find;find sub{/\.mp3$/&&rename$_,substr$_,3},".";

    or

    use File::Find;find{/\.mp3$/&&rename$_,substr$_,3}".";

    if find was declared with proto (&$);

    I tried replacing substr$_,3 with s/..-/&&$_, but it modified the first $_ (weird? isn't the comma operator supposed to evalute left, and then right?)

    bonus - it passes strict.

    Update: Formatting was icky, so I added line breaks.

    -nuffin
    zz zZ Z Z #!perl
      isn't the comma operator supposed to evalute left, and then right?
      But there's no comma operator here - that's a list. A comma is a list element separator in list context and a comma operator in scalar context. There's no such thing as a list in scalar context and no such thing as a comma op in list context.

      Makeshifts last the longest.

      And if the track number is more than 2 digits?

      Update: Oops! It seems that I didn't read the challenge carefully.
      buzzcutbudda specified that the track number would only be two digits.
      Thanks for the heads up, nothingmuch.

        I think I would go for ()=/\d+-(.*)/g

        -nuffin
        zz zZ Z Z #!perl

        The id3v1 spec only has space for a two-digit track number. (IIRC, theoreticaly, you could put any two characters in, so you could count in hex, base-36, or whatever if you need more space, up to a theoretical max of 254**2, with any two chars, other then \0, which probably has special meaning. I don't remember id3 that well off the top of my head.)


        Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

        I believe it's the case that the CD spec itself limits a disc to 99 tracks anyway.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://213944]
Approved by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found