Samy_rio has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am working in the validation of a file. I need to compare the attributes value present in the file with list of values.

I have stored the list of values for different attributes in "%attributes" hash. And I stored the attributes and their values in "%are" hash which are present in the file.

If the attribiute value which is present in the file, is not match with the list of values, then to raise error message.

Input is: <render fo:font-style="italic" fo:fontvariant="noormal">

Output is: (1) ERR :Line 19 Col 94
        Check the render attribute fo:fontvariant="noormal".

while ($file =~/<render([^>]*)>/gi) { my $atri=$1; $pre = $`; $ln= $pre =~s/\n/\n/g; ++$ln; ($pre=~/\n$/)?( $col=0):($col = length((split(/\n/,$p +re))[-1])); %attributes=('fo:font-style'=>["\"normal\"", "\"itali +c\"", "\"oblique\"", "\"backslant\"", "\"inherit\""], 'fo:fontvariant'=>["\"normal\"", "\"small-caps\"", "\ +"inherit\""], 'fo:fontweight'=>["\"normal\"", "\"bold\"", "\"bolder +\"", "\"lighter\"", "\"inherit\"", "\"100\"", "\"200\"", "\"300\"", " +\"400\"", "\"500\"", "\"600\"", "\"700\"", "\"800\"", "\"900\""], 'fo:visibility'=>["\"visible\"", "\"hidden\"", "\"col +lapse\"", "\"inherit\""]); @a= split(/ /,$atri); %are=map{split(/=/,$_)}@a; for (keys (%are)) { $k=$_; if ($k=~/(fo:font\-style|fo:fontvariant|fo:fontweight +|fo:visibility)/) { while($i != 14){ push(@app,$attributes{$_}->[$i]); $i++; } @abc=grep(/$are{$k}/, @app); print FOUT "\n\n(".++$Errno.") ERR :Line ".($ln)." Co +l ".(++$col)."\n\tCheck the render attribute $k=$are{$k}.\n" if ($abc +[0] eq ''); } } }

It works fine. But it is very lengthy proces. That is, pushing the list of values in the array, after that grepping the values etc.

Is there any easy method to do this?

Regards,
Velusamy R

Replies are listed 'Best First'.
Re: How to compare two hashes
by jhourcle (Prior) on Jul 09, 2005 at 13:54 UTC

    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':

      Thank you very much for your reply.

      Regards,
      Velusamy R.

Re: How to compare two hashes
by friedo (Prior) on Jul 09, 2005 at 12:04 UTC
    See "How do I test whether two arrays or hashes are equal?" in perlfaq4.

    Nevermind. Wasn't paying attention, as usual.

      you have misunderstood the question. what i want is to check individual values of both the hashes with same key. Because, some attributes are optional.

      Regards,
      Velusamy R.