DeusVult has asked for the wisdom of the Perl Monks concerning the following question:
So, if you had three guys named John, Jack, and Joe who just changed their names to Mike, Mark, and Moe, %substitute_hash might look like this:
%substitute_hash = ( John => Mike, Jack => Mark, Joe => Moe );
Now the problem proper: I would like to write the following regex:
$string =~ s/(anything which is a key in %substitute_hash/$substitute_hash{the thing found in between the first two /'s}/;
The best thing I can think of is to do the cheap little hackish version:
foreach keys %substitute_hash { $string =~ s/$_/$substitute_hash{$_}; }
I'll do that as a last resort, but the application I need to do this for will involve doing this substitution on many, many files (several dozen, maybe 100+), and %substitute_hash will likely be very big (several hundred entries, maybe even a few thousand), so efficiency is really a factor.
Also, I'm not particularly married to this implementation, so if you can think of a really efficient way of doing this with some other data structure than a hash, that isn't a problem. Thanks in advance.
Some people drink from the fountain of knowledge, others just gargle.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: hashes in regexes
by japhy (Canon) on Mar 29, 2001 at 01:24 UTC | |
by tadman (Prior) on Mar 29, 2001 at 04:12 UTC | |
Re: hashes in regexes
by merlyn (Sage) on Mar 29, 2001 at 01:26 UTC | |
by tilly (Archbishop) on Mar 29, 2001 at 19:32 UTC | |
Re: hashes in regexes
by bjelli (Pilgrim) on Mar 29, 2001 at 05:26 UTC | |
Re: hashes in regexes
by davorg (Chancellor) on Mar 29, 2001 at 13:25 UTC | |
Re: hashes in regexes
by lachoy (Parson) on Mar 29, 2001 at 01:46 UTC | |
by merlyn (Sage) on Mar 29, 2001 at 02:25 UTC | |
Re: hashes in regexes
by satchboost (Scribe) on Mar 29, 2001 at 01:51 UTC |