Hint : it's much easier to traverse deep data structure when iterating over values instead of keys, especially if you never need the keys.

Please note my naming convention using trailing h_ and a_ to denote hash refs and array refs.

i.e. h_codes -> h_planet -> a_aa see included dump

Like this it's obvious when to use @$a_ and %$h_ for dereferencing ...

use strict; use warnings; use Data::Dump qw /pp dd/; my %aas = ( 'serine' => ['TCA', 'TCC', 'TCG', 'TCT'], 'proline' => ['CCA', 'CCC', 'CCG', 'CCT'] ); my %codes; $codes{'earth'} = \%aas; $codes{'mars'} = { 'serine' => ['QWZ', 'QWX', 'QWW'], 'proline' => ['ZXZ', 'ZXX', 'ZXQ', 'ZXW'] }; dd \%codes; # dump for clarity base_code (\%codes); sub base_code { my ($h_codes) = @_; for my $h_planet (values %$h_codes) { for my $a_aa (values %$h_planet) { # for my $codon ( @$a_aa ) { print $codon, "\n"; } } } }

{ earth => { proline => ["CCA", "CCC", "CCG", "CCT"], serine => ["TCA", "TCC", "TCG", "TCT"], }, mars => { proline => ["ZXZ", "ZXX", "ZXQ", "ZXW"], serine => ["QWZ", "QWX", "QWW"], }, } TCA TCC TCG TCT CCA CCC CCG CCT QWZ QWX QWW ZXZ ZXX ZXQ ZXW

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!


In reply to Re: printing complex data structures (values) by LanX
in thread printing complex data structures by ic23oluk

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.