in reply to Custom printing of an HoA

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

Replies are listed 'Best First'.
Re: Re: Custom printing of an HoA
by Limbic~Region (Chancellor) on Oct 03, 2003 at 19:41 UTC
    blokhead,
    You may want to modify your map line:
    my @ith_data = map { defined $data{$_}[$i] ? $data{$_}[$i] : '' } @key +s;
    This way 0 and "0" will not get changed to "".

    Cheers - L~R

Re: Re: Custom printing of an HoA
by bioinformatics (Friar) on Oct 03, 2003 at 19:16 UTC
    This is exactly what I am looking for!! It is a very different way of doing things compared to the direction I was heading, but works very nicely. Always glad to learn new stuff....Thanks!!!
    Bioinformatics