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