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?
Instead of having to do this: $hash{ 'march', '01, 'mar', '01'};
I want to do something like this: $hash{ '/(jan[*]/' } = '01';
Would that work?
ASKER
What does "keys" do exactly?
It returns the keys of the hash, in this case, '/^jan/i'
#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;
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?
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
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_Be rkeley_BMP _side_2.mp 3
June_03_2004_Marriage_Vall ejo_Side_1 .mp3
March_04_1999_Marriage_Val lejo_side_ 1.mp3
March_04_1999_Marriage_Val lejo_side_ 2.mp3
March_16_2004_Repentence_r estoration _dress_cod e_Berkeley _Side_1.mp 3
May_15_2001_Gossip_Baton_R ouge_Side_ 1.mp3
May_15_2001_Gossip_Baton_R ouge_Side_ 2.mp3
Oct_21_1993_The_Spirit_of_ disobedien ce_Berkele y_Side_1.m p3
Oct_21_1993_The_Spirit_of_ disobedien ce_Berkele y_Side_2.m p3
Sept_07_2004_I_Peter_Our_H ope_UC_sid e_1.mp3
Sept_07_2004_I_Peter_Our_H ope_UC_sid e_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
Given a folder with the following file names:
July_20_2004_Friendship_Be
June_03_2004_Marriage_Vall
March_04_1999_Marriage_Val
March_04_1999_Marriage_Val
March_16_2004_Repentence_r
May_15_2001_Gossip_Baton_R
May_15_2001_Gossip_Baton_R
Oct_21_1993_The_Spirit_of_
Oct_21_1993_The_Spirit_of_
Sept_07_2004_I_Peter_Our_H
Sept_07_2004_I_Peter_Our_H
Sept_09_1993_Second_Coming
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;
}
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";
}
@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 '$_ $!;
}
$re=qr/(@{[join'|',keys %hash]})\w*/;
for( <*.mp3> ){
my $f=$_;
s/$re/$hash{lc $1}/g;
rename $f,$_ or warn '$_ $!;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
...
Open in new window