in reply to hash key regular expression pattern

What about something like this?:
%cat = ( 'C[AEIOU]NWAY' => 'AAA', '(?:BOSTON|CHICAGO)' => 'BBB', '(?:LAS VEGAS|NEVADA)' => 'CCC', '(?:DALLAS|AUSTIN)' => 'DDD', '(?:NASHVILLE|M[AEIOU]MPHIS)' => 'EEE' ); $var = "CHICAGO"; @keys = keys %cat; $re = "(".join(")|(", @keys).")"; if ($var =~ /$re/o) { for ( 1 .. @keys) { print "$_:", $$_, " - ", $keys[$_-1], "\n" if defined $$_ } }

Problems:

I think the for loop could be replaced with one of the magical $+ or $- vars, but I couldn't find exactly the one I'm looking for - "The var that tells me how many of $1, $2 were defined in the last match".

-- Dan

Replies are listed 'Best First'.
Re: (z) Re: hash key regular expression pattern
by John M. Dlugosz (Monsignor) on Nov 07, 2002 at 21:39 UTC
    It's $+ (see perlvar).

    Nice idea.