in reply to hash key regular expression pattern
You could run through the keys and do a pattern match on each one, like:
my %cat = ('something' => 'stuff',); my $variable = 'something'; my $match; while (my ($key, $value) = each %cat) { if ($variable =~ /$key/) { $match = $value and last; } }
Unfortunately this really nullifies the primary usefullness of a hash, e.g., efficient and straightforward dictionary lookup. What would be a better solution, if you can change the manner in which you store your data, is to use Tie::Hash::Regex, which enables you to use a regular expression to do key lookups. Very spiffy IMO, and written by a couple of local monks too.
Update: fixed two typos in the code snippet.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: hash key regular expression pattern
by BrowserUk (Patriarch) on Nov 07, 2002 at 10:04 UTC | |
by djantzen (Priest) on Nov 07, 2002 at 10:14 UTC |