From the looks of things, you're attempting to validate CSS. You might want to search on your favorite search engine for 'CSS validator', so you don't have to duplicate work.
As for testing if an item is in a list ... when the list doesn't change, as in your case, you can build a hash with a list as its keys (preferably, outside the loop, or at whatever level it doesn't change), and then just test if the item is a key in the hash.
# before the loop sub enquote { return { map { qq("$_") => undef } @_ }; } my %attributes=( 'fo:font-style' => enquote( qw(normal italic oblique backslant inher +it ) ), 'fo:fontvariant'=> enquote( qw(normal small-caps inherit ) ), 'fo:fontweight' => enquote( qw(normal bold bolder lighter inherit 10 +0 200 300 400 500 600 700 800 900) ), 'fo:visibility' => enquote( qw(visible hidden collapse inherit) ), ); # then, when you're inside the loop: for my $k (keys (%are)) { if ( exists( $attributes{$k} ) ) { if ( ! exists($attributes{$k}{$are{$k}}) ) { print FOUT $errorMessage; } } }
I would also suggest that you select an indenting style, and then stick to it -- it took me longer to understand the logic that you were trying, than to write a response. (better selection of variable names would have been useful, as well)
If the script isn't going to be long running, and/or you're not expecting a whole lot of items that aren't going to be in the test list, you can just set the hash values to a 'true' value, and you can leave out the calls to 'exists':
sub enquote { return { map { qq("$_") => 1 } @_ }; } my %attributes=( 'fo:font-style' => enquote( qw(normal italic oblique backslant inher +it ) ), 'fo:fontvariant'=> enquote( qw(normal small-caps inherit ) ), 'fo:fontweight' => enquote( qw(normal bold bolder lighter inherit 10 +0 200 300 400 500 600 700 800 900) ), 'fo:visibility' => enquote( qw(visible hidden collapse inherit) ), ); ##### for my $k (keys (%are)) { if ( $attributes{$k} and ! $attributes{$k}{$are{$k}} ) { print FOUT $errorMessage; } }
In reply to Re: How to compare two hashes
by jhourcle
in thread How to compare two hashes
by Samy_rio
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |