in reply to Using a regex as a hash key

Either keep your original key $regex around,
my $regex=qr/^Device(\s)+Configuration/; ... my $result = $functions{$regex}->($var2);
or traverse the hash as usual:
my $result = undef; for (keys %functions) { if (this_is_the_key_I_want($_)) { $result = $functions{$_}->($var2); last; } }
The check for this_is_the_key_I_want() is optional of course, or you could scan the regex itself as if it were a string, such as /Config/.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re^2: Using a regex as a hash key
by ikegami (Patriarch) on Jun 06, 2007 at 16:57 UTC
      ikegami, I recognized that it's a TRH, but it still behaves like a hash including iteration, as well as the extra features and odd non-1:1 key behavior. If he doesn't have anything for a key (his missing $val1), he'll have to start somewhere.

      Perhaps I should have said he could just assign $val1 = 'Devices Configuration for Hard Drives' to pick out one particular key, or the hail-mary dispatch loop that citromatik offers where you just throw any old $_ in there and hope it sticks.

      --
      [ e d @ h a l l e y . c c ]

        Sorry, it sounds like I needed to be more explicit.

        %functions is a Tie::RegexpHash, so your suggestion of $functions{$regex}->($var2); is wrong — Use a normal has for that — and a step in the wrong direction. The key for lookups should not be a regexp, but a value to match against the existing keys.

        Looping over all the keys is even worse. The whole point of Tie::RegexpHash is to save you from doing that.