The code below probably satisfies Q1 - it uses sort alfNum to sort the keys for printing (see below for alfNum).

Given the answer to Q1, Q2 it probably moot.

The answer to Q1 is the answer to Q3.

The key is a user provided compare function used by sort. In this case I've provided one called alfNum:

sub alfNum { my ($aAlf, $aNum) = $a =~ /([a-z]+)(\d+)/i; my ($bAlf, $bNum) = $b =~ /([a-z]+)(\d+)/i; return $aAlf cmp $bAlf if $aAlf ne $bAlf; return $aNum <=> $bNum; }

Note that alfNum returns -1 when $a is less than $b, 0 when they are equal, and 1 when $a is greater.

use strict; use warnings; use Data::Dump::Streamer; use strict; use warnings; my %hoh = ( A10 => {bl=>0, ff=>4, gg=>3}, A2 => {cc=>5, dd=>4, ee=>9}, A21 => {cd=>5, de=>4, ef=>9}, B21 => {og=>0, wo=>4, ee=>3}, B3 => {oa=>0, wd=>4, ec=>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 (sort alfNum @{$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"; } sub alfNum { my ($aAlf, $aNum) = $a =~ /([a-z]+)(\d+)/i; my ($bAlf, $bNum) = $b =~ /([a-z]+)(\d+)/i; return $aAlf cmp $bAlf if $aAlf ne $bAlf; return $aNum <=> $bNum; }

Prints:

A2 A10 A21 cc=>5 bl=>0 cd=>5 dd=>4 ff=>4 de=>4 ee=>9 gg=>3 ef=>9 B2 B3 B21 oo=>5 ec=>3 ee=>3 pp=>4 oa=>0 og=>0 zz=>9 wd=>4 wo=>4

DWIM is Perl's answer to Gödel

In reply to Re: HOH again.. and tr/// and more.. by GrandFather
in thread HOH again.. and tr/// and more.. 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.