Re: file renamer (i'm a newbie)
[email protected] (Philippe 'BooK' Bruhat) Thu, 30 Mar 2006 11:25:10 +0200
| Newsgroups | gmane.comp.lang.perl.golf |
|---|---|
| Message-ID | <[email protected]> |
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))