in reply to Matching elements in a array

I'm not sure I understand - it looks like you want to have the "ch(^>*)" in @tags treated as a regex, but you're not actually doing that. Since you can't use a regex as a hash key to do what you want, you might consider using an array of regexes. Also, when matching the tag, you probably want to match up to the first space or >:
my @tag_regexes = ( qr/^kt$/, qr/^bold$/, qr/^ital$/, qr/^ch\d$/, ); while (defined (my $line = <DATA>)) { $line =~ /<([^ >]+)/; my $tag = $1; die "Invalid element $tag in $.. Cannot proceed due to the above e +rror\n" unless grep { $tag =~ $_ } @tag_regexes; print "tag: $tag\n"; } __DATA__ <kt> <bold> <ital> <ch1> <kt someattribute="somevalue"> <bold someattribute="somevalue"> <ital someattribute="somevalue"> <ch1 someattribute="somevalue">

Replies are listed 'Best First'.
Re^2: Matching elements in a array
by duckyd (Hermit) on Aug 23, 2006 at 08:09 UTC
    Actually, I just realized that if you do go this route you can avoid needing to match twice:
    my @tag_regexes = map { qr/<($_) ?[^>]*>/ } qw/kt bold ital ch\d/; while (defined (my $line = <DATA>)) { die "Invalid element in line #$. ($line). Cannot proceed due to th +e above error\n" unless grep { $line =~ $_ } @tag_regexes; } __DATA__ <kt> <bold> <ital> <ch1> <kt someattribute="somevalue"> <bold someattribute="somevalue"> <ital someattribute="somevalue"> <ch1 someattribute="somevalue"> <foo>
Re^2: Matching elements in a array
by duckyd (Hermit) on Aug 23, 2006 at 16:25 UTC
    Actually, I just realized that if you do go this route you can avoid needing to match twice: <code> my @tag_regexes = map { qr/<($_) ?^>*>/ } qw/kt bold ital ch\d/