[Chicago-talk] Using a hash in a regex

Andy Lester andy at petdance.com
Thu Jul 13 13:34:25 PDT 2006


On Jul 13, 2006, at 3:32 PM, Jay Strauss wrote:

> Hi,
>
> How do you use a hash in a regex?  I remember it being mentioned but I
> couldn't find it in the archives.  For example,
>
> %h = (this=>1, that=>2);
>
> $s = "this is a test";
>
> # now what I'd like to do is:
> my $n = m/$s/%h/;  #and have $n = 1;
>
> I can't google how to do it. I know I could loop thru the hash  
> (like I'm
> doing now) but I could have sworn I could use it in the regex.

No, you have to loop manually.

for my $key ( keys %h ) {
     if ( m/$s/$key/ ) {
          $n = $h{$key};
     }
}



--
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance






More information about the Chicago-talk mailing list