Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Compare Hashes and Arrays

by kalyanrajsista (Scribe)
on Dec 16, 2009 at 12:11 UTC ( [id://813009]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I'm trying to compare two hashes and arrays. I didn't find any problem when comparing with hashes where in keys are in whatever order.

I accept that my understanding is wrong and certainly it should not match. But still want to match the arrays where data is same in both array's but order varies

use strict; use Data::Compare; my $h1 = { 'recid2' => '345', 'recid1' => '123' }; my $h2 = { 'recid1' => '123', 'recid2' => '345' }; my @a1 = (1,2,3); my @a2 = (3,2,1); # simple procedural interface print 'structures of $h1 and $h2 are ', Compare($h1, $h2) ? "" : "not ", "identical.\n"; # simple procedural interface print 'structures of $a1 and $a2 are ', Compare(\@a1, \@a2) ? "" : "not ", "identical.\n";

Any suggestions please

Replies are listed 'Best First'.
Re: Compare Hashes and Arrays
by BioLion (Curate) on Dec 16, 2009 at 13:25 UTC

    For arrays you could compute the intersection, and if the size of the intersection array is the same size as both input arrays, then they are identical. List::Compare can be used for getting the intersection.

    You could have your own compare() subroutine which directs to the corret comparison depending on what sort of data is passed and how you want to define 'the same'.

    I didn't find any problem when comparing with hashes where in keys are in whatever order. I accept that my understanding is wrong and certainly it should not match
    Actually the two hashes you give *are* the same. Hash key/value pairs are not stored in the order they are input, and your key/value pairs are the same...

    I haven't had much time to look into the pros/cons of it yet, and i don't know how uesful it might be, but as of 5.10 (i think, at least my 5.8.8 doesn't have the feature) the ~~ smartmatch operator can compare hashes and arrays ( perlfaq4 ), although it seems to have the same issues about ordering of elements as OP and rata have already mentioned...

    As usual, it is all a matter of semantics, and 'identical' can be defined several ways, so like i said, i would have my own little sub that does exactly what *i* define as identical. HTH.

    Just a something something...
Re: Compare Hashes and Arrays
by Ratazong (Monsignor) on Dec 16, 2009 at 12:21 UTC
    A possible solution is to (temporarily) sort the arrays before comparing them ... be however aware that this costs time and space if the arrays are huge...
    HTH Rata
Re: Compare Hashes and Arrays
by Skeeve (Parson) on Dec 16, 2009 at 13:08 UTC

    Provided that either the array's values are unique or provided that you don't care for the number of occurences, you could use the array's values as hash keys. You can either compare the hashes, as you already are able to, or you can try this:

    my @base= qw/b c d e/; my @dist= qw/w x y z/; my @id = @base; my @more= qw/a b c d e/; my @less= qw/b c d/; my @intr= qw/a b c d/; print compare(\@base, \@dist); print compare(\@base, \@id ); print compare(\@base, \@more); print compare(\@base, \@less); print compare(\@base, \@intr); sub compare { my($b, $c)= @_; my %a; @a{@$b}= (); my $n= scalar %a; @a{@$c}= (); return "differ\n" if $n ne scalar %a; delete @a{@$c}; return "differ\n" if scalar %a; return "identical\n"; }


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://813009]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-03-19 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found