It may help to use objects instead of explicit hashes. The hashes are still effectively there and the nested structure is still effectively there, but you can provide methods that give friendly ways of managing the data that hide the underlying complexity. Consider:

#!/usr/bin/perl use strict; use warnings; package SNP; sub new { my ($class, %params) = @_; die "id parameter required by $class constructor\n" if !exists $pa +rams{id}; $params{genes} ||= {}; return bless \%params, $class; } sub addGene { my ($self, $geneId, %params) = @_; $self->{genes}{$geneId} ||= Gene->new(id => $geneId, %params); return $self->{genes}{$geneId}; } sub match { my ($self, $other) = @_; my @otherGenes = sort keys %{$other->{genes}}; my @genes = sort keys %{$self->{genes}}; my $result = ''; return "$self->{id} and $other->{id} differ in number of genes.\n" if @otherGenes != @genes; # different number of transcripts # Check all genes match for my $gene (@genes) { my $matchFail = $self->{genes}{$gene}->match($other->{genes}{$ +gene}); next if !$matchFail; $result .= "- Gene mismatch for $gene:\n"; $result .= $matchFail; } if ($result) { $result = "$self->{id} does not match $other->{id}:\n" . $resu +lt; } return $result; } package Gene; sub new { my ($class, %params) = @_; die "id parameter required by $class constructor\n" if !exists $pa +rams{id}; $params{trans} ||= {}; return bless \%params, $class; } sub addTranscript { my ($self, $transId, %params) = @_; $self->{trans}{$transId} ||= Transcript->new(id => $transId, %para +ms); return $self->{trans}{$transId}; } sub match { my ($self, $other) = @_; my @otherTrans = sort keys %{$other->{trans}}; my @trans = sort keys %{$self->{trans}}; my $result = ''; return "$self->{id} and $other->{id} differ in number of transacti +ons\n" if @otherTrans != @trans; # different number of transcripts # Check all transcripts match for my $transName (@trans) { my $matchFail = $self->{trans}{$transName}->match($other->{trans}{$transNa +me}); next if !$matchFail; $result .= "-- Transcript mismatch for $transName:\n"; $result .= $matchFail; } return $result; } package Transcript; sub new { my ($class, %params) = @_; die "id parameter required by $class constructor\n" if !exists $pa +rams{id}; $params{props} ||= {}; return bless \%params, $class; } sub setProp { my ($self, $prop, $value) = @_; $self->{props}{$prop} = $value; } sub match { my ($self, $other) = @_; my @otherProps = sort keys %{$other->{props}}; my @props = sort keys %{$self->{props}}; my $result = ''; return if @otherProps != @props; # different number of properti +es # Check all properties match for my $propName (@props) { if (!defined $other->{props}{$propName}) { $result .= "$self->{id} has $propName but $other->{id} doe +sn't\n"; next; } if ($self->{props}{$propName} ne $other->{props}{$propName}) { $result .= "--- $self->{id} and $other->{id} property $propName d +iffers:\n"; $result .= " '$self->{props}{$propName}' and '$other->{props}{ +$propName}'\n"; next; } } return $result; } package main; my $snp1 = SNP->new(id => 'SNP1'); my $gene = $snp1->addGene('Gene1'); my $trans = $gene->addTranscript('Trans1'); $trans->setProp(big => 1); $trans->setProp(color => 'blue'); $gene = $snp1->addGene('Gene2'); $trans = $gene->addTranscript('Trans2'); $trans->setProp(big => 1); $trans->setProp(color => 'green'); my $snp2 = SNP->new(id => 'SNP2'); $gene = $snp2->addGene('Gene1'); $trans = $gene->addTranscript('Trans1'); $trans->setProp(big => 1); $trans->setProp(color => 'blue'); $gene = $snp2->addGene('Gene2'); $trans = $gene->addTranscript('Trans2'); $trans->setProp(big => 1); $trans->setProp(color => 'blue'); print $snp1->match($snp2);

Prints:

SNP1 does not match SNP2: - Gene mismatch for Gene2: -- Transcript mismatch for Trans2: --- Trans2 and Trans2 property color differs: 'green' and 'blue'
True laziness is hard work

In reply to Re: can i avoid all these nested hashes by GrandFather
in thread can i avoid all these nested hashes by Anonymous Monk

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.