in reply to Pattern matching across an array

It's hard to tell exactly what your validation criteria really are.
The way I hear it is, all the names in the array must be matched by the regex, or else there's a validation failure.

If all the names are single letters, as in your example, then the regex could look like this: /^[ABCDE]$/. That's known as a "character class".
However, if the names might be longer, e.g. qw/foo bar quux/, then the regex could use "alternation", like so: /^(foo|bar|quux)$/.

Then, to apply the regex (whichever way it's formulated), you could do this:
my @failures = grep { !/$regex/ } @array; if ( @failures ) { warn "Validation failure. Bad names: @failures\n"; }

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 %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.
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.

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
    Ah, no, sorry. I should have said that. A - E represent DB table fields, which are validated by the respective component of the regexp.

    I can't use character classes or alternation simply because each attribute has separate validation criteria. I'm trying to concatenate those critera into one regular expression which I can then apply to the whole array, rather than having to store a set of separate regexps for each attribute on the DB.

    Changing $" to '' seems to have done the trick .. thanks to everyone for the help.

    -- Foxcub
    A friend is someone who can see straight through you, yet still enjoy the view. (Anon)

      Ah, I see.
      Well, I question the wisdom of your approach... but if that's the way you want to do it, then you should at least make sure that the character you're using to separate the values doesn't occur in any of the values.

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.