Here's a rather more simple (but no less lengthy ;-) attempt at creating a data structure signature.
package Data::AsString; require Carp; use strict; use warnings; use overload q("") => sub { shift->as_string() }; sub new { my $class = shift; my $self = {}; $self->{struct} = shift; $self->{info} = { depth => 0 }; return bless $self, $class; } sub as_string { my $self = shift; $self->{'ref'} = $self->_traverse($self->{struct}); $self->{string} = $self->_collapse_info(); return $self->{string}; } sub _traverse { my $self = shift; my $ds = shift; my @values = (); if(ref($ds) eq 'HASH') { @values = values %$ds; } elsif(ref($ds) eq 'ARRAY') { @values = @$ds; } else { Carp::croak("can't iterate through a ".ref($ds)); } foreach my $el (@values) { if(ref($el) eq 'HASH' or ref($el) eq 'ARRAY') { my $key = "#".$self->{info}->{depth}; push @{$self->{info}->{$key}}, ref($el); $self->{info}->{depth}++; push @{$self->{info}->{$key}}, $self->_traverse($el); $self->{info}->{depth}--; } } return ref $ds; } sub _collapse_info { my $self = shift; my $str = substr($self->{'ref'}, 0, 1); for my $lvl (sort grep /^#\d+/, keys %{$self->{info}}) { my %uniq = (); $uniq{$_}++ for @{$self->{info}->{$lvl}}; my @type = keys %uniq; if(@type > 1) { Carp::carp("data is not homogeneous at depth $lvl"); $str .= '['.join("o", map { substr($_, 0, 1) } @type).']'; } else { $str .= 'o' . substr($type[0], 0, 1); } } return $str; } qq(and I'm spent); package main; use Data::AsString; my $data = { foo => { one => [qw(x y z)] }, bar => { two => [qw(a b c)] } }; my $sig = Data::AsString->new($data); print "signature of \$data is $sig\n";

HTH

broquaint


In reply to Re: Checking whether two variables have the same structure by broquaint
in thread Checking whether two variables have the same structure by Corion

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.