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"; }