Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
Could any one help me in printing the hashes of hashes? the data sturcture is something like this.
$VAR1 = { 'LB2241-RCC' => { 'rs6557634' => { 'plex' => 'W30467', 'gt' => 'T', 'file' => ' file1.csv' }, 'rs1032807' => { 'plex' => 'W30569', 'gt' => 'G', 'file' => ' file1.csv' },
where LB2241-RCC is a sample, which can have around 60 -89 similar rs6557634 (called snp-ids) and each of them in turn have the the three keys, plex, gt, file. I would like to print them in this format:
W30467,LB2241-RCC,T,T,T,C,CA,GT,T,GA,C,A,G,CT,T,T,CA,AG,TC,G,GA,C,TC,C +T,AG,AG,C,GT,GA,G,GA,T W30469,LB2241-RCC,G,T,T,C,CA,GT,T,GA,C,A,G,CT,T,T,CA,AG,TC,G,GA,C,TC,C +T,AG,AG,C,GT,GA,G,GA,T
In simple words, I have to get all the snps-ids->gt foreach of the sample and their plex. The same sample could be seen with a different snp-ids and a different 'gt'. Any help in how to print them ? Thanks in advance, your help is much appreciated.

Replies are listed 'Best First'.
Re: fetching from a hash
by GrandFather (Saint) on May 07, 2011 at 11:59 UTC

    I don't see where all the data in the output is represented by the hash contents. Perhaps you would like to put together a sample script that populates the hash with enough information to be able to generate the output you show and show us that?

    True laziness is hard work
Re: fetching from a hash
by JavaFan (Canon) on May 07, 2011 at 14:05 UTC
    Assuming the data is in $hash.
    while (my ($key1, $data1) = each %$hash) { while (my ($key2, $data2) = each %$data1) { printf "%s,%s-\nRCC,%s,T,T,C,CA,GT,T,GA,C,A,G,CT," . "T,T,CA,AG,TC,G,GA,C,TC,CT,AG,AG,C,GT,GA,G," . "GA,T", $$data2{plex}, $key1, $$data2{gt}; } }
Re: fetching from a hash
by CountZero (Bishop) on May 07, 2011 at 15:06 UTC
    Assuming you want to include the contents of the file named in your file" field, the script could be as follows:
    use Modern::Perl; my %data = ( 'LB2241-RCC' => { 'rs6557634' => { 'plex' => 'W30467', 'gt' => 'T', 'file' => 'file1.csv' }, 'rs1032807' => { 'plex' => 'W30569', 'gt' => 'G', 'file' => 'file1.csv' }, } ); foreach my $sample ( keys %data ) { foreach my $snpid ( keys %{ $data{$sample} } ) { my $plex = $data{$sample}->{$snpid}{'plex'}; my $gt = $data{$sample}->{$snpid}{'gt'}; my $file = $data{$sample}->{$snpid}{'file'}; open my $FH, '<', "./$file" or die $!; my $content = join ', ', <$FH>; say "$plex, $sample, $gt, $content"; } }
    Output:
    W30569, LB2241-RCC, G, T,T,C,CA,GT,T,GA,C,A,G,CT,T,T,CA,AG,TC,G,GA,C,T +C,CT,AG,AG,C,GT,GA,G,GA,T W30467, LB2241-RCC, T, T,T,C,CA,GT,T,GA,C,A,G,CT,T,T,CA,AG,TC,G,GA,C,T +C,CT,AG,AG,C,GT,GA,G,GA,T

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: fetching from a hash
by tchrist (Pilgrim) on May 08, 2011 at 00:48 UTC
    Just a suggestion. Instead of using Data::Dumper, whose output is really ugly, you might consider using the CPAN Data::Dump module instead. Watch what it does with this structure from the perldsc(1) manpage:
    use strict; use warnings; use Data::Dump; my $HoH = { flintstones => { lead => "fred", pal => "barney", }, jetsons => { lead => "george", wife => "jane", "his boy" => "elroy", }, simpsons => { lead => "homer", wife => "marge", kid => "bart", }, }; dd $HoH;
    produces this:
    { flintstones => { lead => "fred", pal => "barney" }, jetsons => { "his boy" => "elroy", "lead" => "george", "wife" => + "jane" }, simpsons => { kid => "bart", lead => "homer", wife => "marge" }, }
    Isn’t that much tidier?