T
Tim Shoppa
I often find myself with a list of things that I'm searching for. And
for each of the things I'm searching for, there's an action I want to
do.
Sometimes the "search for" pattern is just the first four characters in
the line, for example. Here things are easy: I build a hash with the
key being the four-character pattern, and the value being the
subroutine to execute. Works very nicely: get each line, use a
substr() to extract the first four characters, look them up in the
hash, and execute the correct subroutine. Very quick, very fast, very
idiomatic.
But other times the patterns are not so easily handled. Often they are
true regexp's, matching variable repeats/patterns. This of course can
be handled with if matches and blocks to do the actions, but this
screams out to me as something that I ought to be able to handle using
a data structure which is something like a hash, using regexp's as
keys.
Pages 193/194 of the Camel book reveal how to loop over a bunch of
precompiled regexp's, using qr// to precompile the regexp's, and this
isn't bad. But it's not quite the same as a hash lookup. And it seems
to me that there ought to be an idiom, maybe a CPAN module, that makes
the whole operation look more like a hash lookup, because that's how I
think of it in my head, even though I know that regexp's aren't really
as quick or efficient as simple keys.
So, is there a common perl idiom for dealing with this situation?
Maybe a CPAN module?
Tim.
for each of the things I'm searching for, there's an action I want to
do.
Sometimes the "search for" pattern is just the first four characters in
the line, for example. Here things are easy: I build a hash with the
key being the four-character pattern, and the value being the
subroutine to execute. Works very nicely: get each line, use a
substr() to extract the first four characters, look them up in the
hash, and execute the correct subroutine. Very quick, very fast, very
idiomatic.
But other times the patterns are not so easily handled. Often they are
true regexp's, matching variable repeats/patterns. This of course can
be handled with if matches and blocks to do the actions, but this
screams out to me as something that I ought to be able to handle using
a data structure which is something like a hash, using regexp's as
keys.
Pages 193/194 of the Camel book reveal how to loop over a bunch of
precompiled regexp's, using qr// to precompile the regexp's, and this
isn't bad. But it's not quite the same as a hash lookup. And it seems
to me that there ought to be an idiom, maybe a CPAN module, that makes
the whole operation look more like a hash lookup, because that's how I
think of it in my head, even though I know that regexp's aren't really
as quick or efficient as simple keys.
So, is there a common perl idiom for dealing with this situation?
Maybe a CPAN module?
Tim.