Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re:{3} How to test equality of hashes?

by jeroenes (Priest)
on Jun 20, 2001 at 18:26 UTC ( #90004=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: How to test equality of hashes?
in thread How to test equality of hashes?

Nice. I can't see this kind of follow-ups often enough!

A few comments, naturally:

  1. use strict warnings and diagnostics or die. These are your friends on the long run.
  2. You declare 2 vars without need:
    my %h1 = %{ $_[0] }; #h2 similar
  3. Check whether your refs are defined:
    my %h1 = (defined $_[0] and ref $_[0]) ? %{ $_[0] } : ();
    This will make the code more portable/general/cleaner. You may add an extra check for hashiness.
  4. I just realized that this code can't distinguish between undefined or empty, so the grep becomes:
    scalar grep { my ($a, $b) = ( $h1{$_}, $h2{$_} ); ( not defined $a and defined $b ) or ( not defined $b and defined $a ) or $a ne $b } @k1
    Had to think extra carefully there!

Hope this helps,

Jeroen


Comment on Re:{3} How to test equality of hashes?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://90004]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2015-07-02 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (46 votes), past polls