in reply to hash key regular expression pattern

Try this

#! perl -sw use strict; my %cat = ( 'C[AEIOU]NWAY' => 'AAA', '(BOSTON|CHICAGO)' => 'BBB', '(LAS VEGAS|NEVADA)' => 'CCC', '(DALLAS|AUSTIN)' => 'DDD', '(NASHVILLE|M[AEIOU]MPHIS)' => 'EEE', ); my $variable = 'CHICAGO'; for (keys %cat) { print $cat{$_}, $/ if $variable =~ /$_/; } __END__ #Output c:\test>211023 BBB c:\test>

Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy

Replies are listed 'Best First'.
Re: Re: hash key regular expression pattern
by FamousLongAgo (Friar) on Nov 07, 2002 at 14:31 UTC
    Two suggestions:

    1. Precompile the regexs
      qr/C[AEIOU]NWAY/ => 'AAA' ...
    2. Consider storing the regexes in an array, and keeping a separate lookup hash for the values ( or using a Tied hash whose key order you can control ). This will let you put the most common cases first, assuming you exit the loop on the first match, and that you are optimizing for speed.