I'm having some trouble understanding exactly what you mean, but my best guess is that you want something like this:
key1 key2 key3 key4 key5 $data{key1}[0] $data{key2}[0] $data{key3}[0] $data{key4}[0] $data{ +key5}[0] $data{key1}[1] $data{key2}[1] $data{key3}[1] $data{key4}[1] $data{ +key5}[1] $data{key1}[2] $data{key2}[2] $data{key3}[2] $data{key4}[2] $data{ +key5}[2] ...
To do this, you'll need to use map to pull out the appropriate array members for each row:
my $col_width = 20; my @keys = sort keys %data; my $p_format = join(" " => ("%${col_width}s") x @keys) . "\n"; ## $p_format would be "%20s %20s %20s ... %20s\n", so the columns will ## line up even if the data strings are different sizes printf $p_format, @keys; my $i = 0; LOOP: { ## important part: pull out the ith entry in each array my @ith_data = map { $data{$_}[$i] || "" } @keys; ## you mentioned the data strings end in \n chomp @ith_data; printf $p_format, @ith_data; $i++; ## loop again if any array has elements left for (@keys) { redo LOOP if exists $data{$_}[$i]; } }
Thank heavens for bare-block flow control ;) If that scares you, the only reason I used it is in case the arrays weren't all the same length. If yours are all the same known sizes, you can replace that goofy block with for my $i (0 .. $size-1).

I used printf to print things out in aligned columns, but if your data strings are all the same length, you could just as easily use print "@ith_data\n"... although that would start looking weird if some arrays run out of elements early. But if your arrays are all the same size, and your data strings are all the same length, you could get this code down to:

my @keys = sort keys %data; for my $i ( 0 .. $size-1 ) { my @ith_data = map { $data{$_}[$i] || "" } @keys; chomp @ith_data; print "@ith_data\n"; }

blokhead


In reply to Re: Custom printing of an HoA by blokhead
in thread Custom printing of an HoA by bioinformatics

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.