The unasked question here is: Once you have the match, how will you determine what action to take?

If you are reading the patterns to be matched from a separate file prepared by someone else, how do they indicate what action should be taken? Or how do you decide what action should be taken for each pattern matched?

could be arbitrarily complex - ... I don't think the matching text will be much good for a key to a hash.

The matching text may not be a good key as it wouldn't match a hash key that was a non-constant regex, but using $#- will tell you which pattern was matched, and could be used as an index to an array containing the original patterns to retrieve the one that matched (rather than the text it matched).

Eg.

my @patterns = slurp( 'file' ); my %dispatch; @dispatch{ @patterns } = ( ??? ); my $regex = '(', join( ')|(', @patterns ) . ')'; while( my $data = <DATA> ) { if( $data =~ $regex ) { $dispatch{ $patterns[ $#- ] }->( $1 ); } }

The problem with the above code is what to substitute for '???'. Ie. What action is required for matches against each pattern. But that problem exists whether you are using a dispatch table; an if/else cascade or any other mechanism. Of the choices available the dispatch table is by far the easiest--if not the only--option for the dynamic search patterns you describe.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Finding out which of a list of patterns matched by BrowserUk
in thread Finding out which of a list of patterns matched by lemnisca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.