develooper Front page | | Postings from March 2006

Re: file renamer (i'm a newbie)

Thread Previous | Thread Next
March 30, 2006 02:25
Re: file renamer (i'm a newbie)
Message ID:
Le samedi 25 mars 2006 à 00:07, Eric Waguespack écrivait:
> You are really helpful... thanks for all the info.
> here is my latest code... now l'll take a few minutes to digest your
> suggestions.
>    # fix odd extensions
>    $new =~ s/\.mpeg$/\.mpg/;
>    $new =~ s/\.ram$/\.rm/;
>    $new =~ s/\.qt$/\.mov/;
>    $new =~ s/\.jpeg$/\.jpg/;

To handle lots of extensions without having to edit your code, I'd suggest
using a dispatch table.

    # initialise the table
    my %ext = (
        mpeg => 'mpg',
        ram  => 'rm',
        qt   => 'mov',
        jpeg => 'jpg',

    # build up a single regexp
    my $ext_re = qr/\.(@{[join'|', sort keys %ext]})$/;

and in your function:

    # fix odd extensions
    $new =~ s/$ext_re/$ext{$1}/;

When your list of extensions grows, you only have to update the table.
And you can imagine a script that reads the table content from a
configuration file, which gives you even more flexibility.

By the way, golfers, do we have a name for the secret circumfix
@{[]} operator?

 Philippe "BooK" Bruhat

 Even the most powerful being is at the mercy of the weakest.
                                    (Moral from Groo The Wanderer #34 (Epic))

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About