First, don't use prototypes unless you know why you need them. Also, in Perl, you do not need to pre-declare your subroutines like you do in C for instance.

Now, you were trying to use $planet as a hash reference, but it's actually a string (which you extract when you call keys %$h_ref. So, you have to pass in a reference to a hash in the call to base_code(), then inside the sub, you need to use the key name $planet as the key to the encompassing $h_ref to extract out the data you want:

use strict; use warnings; 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'] }; base_code (\%codes); sub base_code { my $h_ref = $_[0]; for my $planet (keys %$h_ref){ for my $aa (keys %{ $h_ref->{$planet} }){ for my $codon (@{ $h_ref->{$planet}{$aa} }){ print $codon, "\n"; } } } }

Output:

CCA CCC CCG CCT TCA TCC TCG TCT ZXZ ZXX ZXQ ZXW QWZ QWX QWW

In reply to Re: printing complex data structures by stevieb
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.