[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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?
0
ifeatu
Asked:
ifeatu
  • 4
  • 4
  • 3
1 Solution
 
tdlewisCommented:
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

0
 
ifeatuAuthor Commented:
What does "keys" do exactly?
0
 
tdlewisCommented:
It returns the keys of the hash, in this case, '/^jan/i'
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ozoCommented:

#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;
0
 
ifeatuAuthor Commented:
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?


0
 
ozoCommented:
http:#26121420 appears to fulfill your stated goal
0
 
ifeatuAuthor Commented:
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

0
 
tdlewisCommented:
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

0
 
ozoCommented:
@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 '$_ $!;
}
0
 
ozoCommented:
# sorry, that should have been more like
@hash{qw(jan feb march apr may june july aug sept oct nov dec)}=('01'..'12');
$re=qr/(@{[join'|',keys %hash]})[a-z]*/i;
for( <*.mp3> ){
  my $f=$_;
  s/$re/$hash{lc $1}/ and (rename $f,$_ or warn "$_ $!");
}
0
 
ifeatuAuthor Commented:
...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now