To do this, you'll need to use map to pull out the appropriate array members for each row: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] ...
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).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]; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |