If you have to find the key in your hash given a value, then perhaps your hash is the wrong way around.
As long as your values are valid as hash keys, you can easily swap keys and values with
my %reversed = reverse %hash;
after which you can find the key corresponding to a value in your original hash, as follows:
my $value = 'whatever';
my $key = $reversed{$value};
| [reply] [d/l] [select] |
As indicated by the first reply, a single regex consisting of 100 values as alternates is not such a big load, really. And you don't even need a special module to do it this way:
my $value_regex = join( '|', values %msgDefn ); # actually, use anony
+monk's version below...
while ( <FILEHANDLE> ) {
print if ( /$value_regex/ );
}
That assumes that the values in your hash are all "safe", in the sense that they don't contain any regex magic characters, like brackets, *, ?, +, period, slash, backslash, and so on.
If the values might contain things of that sort, you could handle it like this (but YMMV, depending on what's really in your data):
my $value_regex = join( '|', map { '\Q'.$_.'\E' } values %msgDefn );
Now, if you ultimately need to know which hash key contains the value that actually matched a given line from the file, then you'd really want to build a reverse hash, as suggested in the 2nd reply.
Update (forgot to mention): Naturally, lots of other caveats apply, such as false-alarm matches on substrings (e.g. a value like "table", treated as above or as in the OP, would match on a line that contains "stable" or "tablet", which might not be what you want. | [reply] [d/l] [select] |
join '|', map quotemeta, values
| [reply] [d/l] |
Thanks everyone...
It helped me a lot. The processing time reduced from 25 seconds to 1 second!!!!.
| [reply] |
use Regex::PreSuf;
my $bigregex = presuf( values %msgDefn );
my $start = time();
while( <FILEHANDLE> ){
print if /$bigregex/;
}
my $end = time();
print "Time taken was ", ($end - $start), " seconds";
| [reply] [d/l] |