in reply to Structured Printing of Hash of Arrays

Untested, assuming %TOP for the top...
my @keys = sort keys %TOP; my $format = join(" ", ("%20s") x @keys)."\n"; printf $format, @keys; for my $line (0..$#{$TOP{$keys[0]}}) { printf $format, map {$TOP{$_}[$line]} @keys; }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
(tye)Re: Structured Printing of Hash of Arrays
by tye (Sage) on Oct 12, 2000 at 01:44 UTC

    I was just going to mention that "%-20s" is probably more what was wanted than "%20s" (yes, annoying how printf defaults to right-justifaction for non-numeric formats). Then I noticed no double underlines and that you assume that all of the arrays have the same number of elements. Finally, I went with "%-19s" which fits nicely on screens that are a multiple of 20 characters wide.

    my @keys= sort keys %TOP; my $format= join( " ", ("%-19s") x @keys ) . "\n"; printf $format, @keys; printf $format, map { "=" x length($_) } @keys; my $line= 0; while( grep { $line <= $#{$TOP{$_}} } @keys ) { printf $format, map { def($TOP{$_}[$line]) } @keys; $line++; } sub def { return defined $_[0] ? $_[0] : def($_[1],""); }

            - tye (but my friends call me "Tye")