The following first builds a hash of alpha keys containing the individual keys, then uses that to generate the table.
use strict; use warnings; my %hoh = ( A1 => {bl=>0, ff=>4, gg=>3}, A2 => {cc=>5, dd=>4, ee=>9}, B1 => {og=>0, wo=>4, ee=>3}, B2 => {oo=>5, pp=>4, zz=>9}, ); my %alphaKeys; push @{$alphaKeys{(/^([a-z]+)/i)[0]}}, $_ for keys %hoh; keys %hoh; for (sort keys %alphaKeys) { my @lines; for my $key (@{$alphaKeys{$_}}) { my $index = 0; $lines[$index++] .= sprintf "%-20s", $key; my %items = %{$hoh{$key}}; $lines[$index++] .= sprintf "%-20s", "$_=>$items{$_}" for sort + keys %items; } print "$_\n" for @lines; print "\n"; }
Prints:
A1 A2 bl=>0 cc=>5 ff=>4 dd=>4 gg=>3 ee=>9 B1 B2 ee=>3 oo=>5 og=>0 pp=>4 wo=>4 zz=>9
Update: changed map to for per Tye's suggestion
In reply to Re: Hash printing problem
by GrandFather
in thread Hash printing problem
by tamaguchi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |