hello symgryph,

so you need a sort of dispatch table of regexes? you can easily store your regexes into hashes values using the qr operator.

Using an hash is the right thing unless you need the order to be preserved: if the case, you need to save the order into another datastructure, probably an array.

Consider this silly example (run it and see how silly it is against the last line: Male is counted as name and as gender too!)

use strict; use warnings; my %look = ( name => qr/([A-Z]\w+\s?)+/, gender=> qr/[fF]?e?[mM]ale/, tel => qr/\d{5,8}/, ); sub validate{ my($str,$pattern)=@_; my $count = () = $str =~ /$pattern/g; return $count; } while (defined (my $line = <DATA>)){ chomp $line; print qq("$line" contains:\n); foreach my $pattern (keys %look){ my $res = validate($line, $look{$pattern}); print "\t$res $pattern\n" } } __DATA__ Kurt Perlish male 5555555 21212121 Mary Perl 6565656 me Male 12042016

L*

Update: never mind for horrid names: in Perl Hash's keys names are forced to be stringified, so you can have spaces, dots and other garbage with no problems.

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Using a 'hash' of regexes, then seeing if they match based upon a 'split' mapped to an array? by Discipulus
in thread Using a 'hash' of regexes, then seeing if they match based upon a 'split' mapped to an array? by symgryph

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.