I had a need for something similar yesterday and throw together a compare routine that recursively walks down the structure. and returns my idea of < = or >, even though I only needed it for determining if 2 structs were the same.
sub isnum($) { $_[0] =~ m{^\s* [-+\d.]* [\d.]+ (?:e[-+]\d+)? \s* $}x } sub Cmp (;$$$); sub Cmp (;$$$) { my $r=0; require P; my ($a, $b, $d) = @_ ? @_ : ($a, $b); my ($ra, $rb) = (ref $a, ref $b); my ($ta, $tb) = (typ $a, typ $b); P::Pe("ta=%s, tb=%s", $ta, $tb) if $d; P::Pe("ra=%s, rb=%s", $ra, $rb) if $d; my ($dta, $dtb) = (defined $ta, defined $tb); # first handle "values" (neither are a type reference) unless($dta || $dtb) { $r = isnum($a) && isnum($b) ? $a <=> $b : $a cmp $b; P::Pe("isnum, a=%s, b=%s, r=%s", isnum($a), isnum($b), $r) if $d; return $r } # then handle unequal type references elsif ($dta ^ $dtb) { return (undef, 1) } elsif ($dta && $dtb && $ta ne $tb) { return (undef, 2) } # now, either do same thing again, or handle differing classes # the no-class on either implies no type-ref on either & is handled +above my ($dra, $drb) = (defined $ra, defined $rb); if ($dra ^ $drb) { return (undef, 3) } elsif ($dra && $drb && $ra ne $rb) { return (undef, 4) } # now start comparing references: dereference and call Cmp again if ($ta eq SCALAR) { return Cmp($$a, $$b) } elsif ($ta eq ARRAY) { P::Pe("len of array a vs. b: (%s <=> %s)", @$a, @$b) if $d; return $r if $r = @$a <=> @$b; # for each member, compare them using Cmp for (my $i=0; $i<@$a; ++$i) { P::Pe("a->[i] Cmp b->[i]...\0x83", $a->[$i], $b->[$i]) if $d; $r = Cmp($a->[$i], $b->[$i]); P::Pe("a->[i] Cmp b->[i], r=%s", $a->[$i], $b->[$i], $r) if $d; return $r if $r; } return 0; # arrays are equal } elsif ($ta eq HASH) { my @ka = sort keys %$a; my @kb = sort keys %$b; $r = Cmp(0+@ka, 0+@kb); P::Pe("Cmp #keys a(%s) b(%s), in hashes: r=%s", 0+@ka, 0+@kb, $r) +if $d; return $r if $r; $r = Cmp(\@ka, \@kb); P::Pe("Cmp keys of hash: r=%s", $r) if $d; return $r if $r; my @va = map {$a->{$_}} @ka; my @vb = map {$b->{$_}} @kb; $r = Cmp(\@va, \@vb); P::Pe("Cmp values for each key, r=%s", $r) if $d; return $r; } else { P::Pe("no comparison for type %s, ref %s", $ta, $ra) if $d; return (undef,5); ## unimplemented comparison } }
Please note, it's raw code. It works on the nested data structures I've tried it on, but I haven't developed any general test cases for it -- and am not sure if I'd want to put it on cpan and if so, where. For now, I added it to my Types::Core module, as it's comparing typed data (a tenuous reason, but with it so small, and not sure where else I'd put it...eh(?)). If you decide to use it, PLEASE tell me about any bugs/problems, so I can develop tests and upgrade the code, but I just wrote it yesterday and don't even know if I want to publish it. There may be similar modules in CPAN, but I wanted something short & sweet and this did exactly what I wanted.

Takes up to 3 params: 1st two are refs to the data structures. If passed no refs, it will use '$a and $b' as starting points (global compare vars). Third param '$d' stands for debug and controls the printing of various progress messages as it goes along.

Literally, I'm using to test to see if some routines internal to a program generate correct results. I.e. the code generating the routines was complicated enough, that I wanted to test it separately -- calling the routines and having them generate various data structures. I needed a way to compare structures that should be equal.

It sounded like you were wanting exactly the same thing I was doing, if not, sorry for the waste of bandwidth and misunderstanding what you wanted, but if it works for you, cool!

Linda


In reply to Re: Data Object compares? by perl-diddler
in thread Data Object Verification Modules? by mwb613

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.