in reply to Pattern matching across an array
Now, if what you're really wanting to do is ascertain that the given names are all in an "approved" set of names, a regex may not be the clearest way of implementing this. I'd recommend using a hash as a set, and using the exists function to determine set membership.my @failures = grep { !/$regex/ } @array; if ( @failures ) { warn "Validation failure. Bad names: @failures\n"; }
Of course, this only works neatly when the names are all exact. If there is any "fuzziness", such as what you might use wildcards for, then regexes are probably the way to go.my %valid_names; @valid_names{ qw/ A B C D E / } = (); # define the set of valid name +s. my @failures = grep { ! exists $valid_names{$_} } @array; # and so forth.
jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Pattern matching across an array
by Tanalis (Curate) on Feb 05, 2003 at 11:53 UTC | |
by jdporter (Paladin) on Feb 05, 2003 at 12:29 UTC |