I think it would make things easier to have a single HoH keyed by your "flag" values with sub-hashes of "contig" and count key/value pairs. This would facilitate accessing everything via keys rather than having to pass separate hashes into a subroutine. It might also look a bit neater if you use printf to make columns line up. Something along these lines perhaps?

knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E ' > my %counts = ( > Bact => { A => 3, B => 7, C => 6 }, > Euk => { B => 5, C => 9 }, > Proph_Vir => { A => 8, C => 4, D => 2 }, > ); > my @sortedKeys = do { > my %seen; > sort grep { not $seen{ $_ } ++ } > map { keys %{ $counts{ $_ } } } > keys %counts; > }; > > printf qq{%-10s%4s%4s%4s%4s\n}, q{}, @sortedKeys; > foreach my $flag ( sort keys %counts ) > { > printf qq{%-10s%4s%4s%4s%4s\n}, > $flag, > map { exists $counts{ $flag }->{ $_ } > ? $counts{ $flag }->{ $_ } > : 0 > } @sortedKeys; > }' A B C D Bact 3 7 6 0 Euk 0 5 9 0 Proph_Vir 8 0 4 2 knoppix@Microknoppix:~$

A minor niggle with your code; why chomp comment lines and then discard them? It would make more sense to swap the two lines so you only chomp data lines.

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: print multiple hashes as multiple columns by johngg
in thread print multiple hashes as multiple columns by AWallBuilder

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.