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


DWIM is Perl's answer to Gödel

In reply to Re: Hash printing problem by GrandFather
in thread Hash printing problem by tamaguchi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.