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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |