Link to home
Start Free TrialLog in
Avatar of ifeatu
ifeatu

asked on

Wondering if you can use regex in a hash?

Is it possible to use regex in a hash... basically I want to do a hash for every month in the year to convert it to the corresponding number (jan = 01, feb = 02...)

Instead of having to do this: $hash{ 'march', '01, 'mar', '01'};

I want to do something like this: $hash{ '/(jan[*]/' } = '01';

Would that work?
Avatar of tdlewis
tdlewis
Flag of United States of America image

This will work:
my %HASH;
$HASH{'/^jan/i'} = '01';
# etc.
$HASH{'/^dec/i'} = '12';

foreach my $key (keys %HASH)
{
  print "$key: $HASH{$key}\n" if eval('"March" =~ ' . $key);
}

Open in new window

Avatar of ifeatu
ifeatu

ASKER

What does "keys" do exactly?
It returns the keys of the hash, in this case, '/^jan/i'
Avatar of ozo

#I'm not sure what you're expecting  $hash{ '/(jan[*]/' } = '01'; to do
#are you trying to do something lilke
@hash{qw(jan feb mar apr may jun jul aug sep oct nov dec)}=('01'..'12');
$_='January February';
$re=qr/(@{[join'|',keys %hash]})\w*/i;
s/$re/$hash{lc $1}/g;
print;
Avatar of ifeatu

ASKER

I'm sorry but this seriously sounds foreign to me...let me break down what I "think" I understand and perhaps you all can fill in the blanks...

My GOAL is to create a hash that I can put something like $_ = Jan; or $_ = January; into and get back a '01' ...because I have a serious of lines with inconsistant naming conventions...(Jan, Janu, January etc.) So tdlewis, you're saying that I initialize the hash (my %HASH;) create a matching statement and take in results ($HASH{'/^jan/i'} = '01';)

This is the part that mistifys me:
foreach my $key (keys %HASH)
{
  print "$key: $HASH{$key}\n" if eval('"March" =~ ' . $key);

So to my understanding a "key" is the value that I passed to the hash (like Jan)
so, in the case of "jan" and "01" [$HASH{$key}] would return a '01' and $key would return 'Jan" right?


http:#26121420 appears to fulfill your stated goal
Avatar of ifeatu

ASKER

Your code is totally not working for me ozo I've tried it in as many ways as I can imagine and it simply will not produce anything close to what I want:


Given a folder with the following file names:
July_20_2004_Friendship_Berkeley_BMP_side_2.mp3
June_03_2004_Marriage_Vallejo_Side_1.mp3
March_04_1999_Marriage_Vallejo_side_1.mp3
March_04_1999_Marriage_Vallejo_side_2.mp3
March_16_2004_Repentence_restoration_dress_code_Berkeley_Side_1.mp3
May_15_2001_Gossip_Baton_Rouge_Side_1.mp3
May_15_2001_Gossip_Baton_Rouge_Side_2.mp3
Oct_21_1993_The_Spirit_of_disobedience_Berkeley_Side_1.mp3
Oct_21_1993_The_Spirit_of_disobedience_Berkeley_Side_2.mp3
Sept_07_2004_I_Peter_Our_Hope_UC_side_1.mp3
Sept_07_2004_I_Peter_Our_Hope_UC_side_2.mp3
Sept_09_1993_Second_Coming_Berkeley_Side_1.mp3

I want to take the month from the name of the track (Oct) and assign it to a variable, then plug that variable into the hash and change the month to a number (10).

I will be using the month number later on in the code so I need it to be assigned to a variable




while (<.mp3>) {

$names = $_;

@hash{qw(jan feb march apr may june july aug sept oct nov dec)}=('01'..'12');
$_='January February';
$re=qr/(@{[join'|',keys %hash]})\w*/i;
s/$re/$hash{lc $1}/g;
print;

}

Open in new window

Try this:
my %MON = ('jan' => '01', 'feb' => '02', 'mar' => '03',
           'apr' => '04', 'may' => '05', 'jun' => '06',
           'jul' => '07', 'aug' => '08', 'sep' => '09',
           'oct' => '10', 'nov' => '11', 'dec' => '12');

while (<.mp3>) {
  my $mon = $MON{lc(substr($_,0,3))};
  print "$mon $_\n";
}

Open in new window

@hash{qw(jan feb march apr may june july aug sept oct nov dec)}=('01'..'12');
$re=qr/(@{[join'|',keys %hash]})\w*/;
for( <*.mp3> ){
my $f=$_;
  s/$re/$hash{lc $1}/g;
   rename $f,$_ or warn '$_ $!;
}
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ifeatu

ASKER

...