in reply to Using a 'hash' of regexes, then seeing if they match based upon a 'split' mapped to an array?
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.
|
|---|