in reply to HOH again.. and tr/// and more..

First, please note that hashes are not ordered like arrays are - you cannot sort them and maintain the order within the hash.

Q1: perldsc has some examples of how to sort a %HoH based on the internal keys. Without being more specific, though, I can only guess as to what you intend. I'll assume you want to sort the internal keys within each outer key. In that case, something like:

while( my ( $outkey, $href ) = each %HoH ) { foreach my $inkey ( sort { $a <=> $b } keys %{ $href } ) { # do stuff here
should work. If you don't understand what is going on in that snippet, see each, sort, and keys for starters, do some searching (see below) as required, and feel free to come back and ask for explanation if needed.

Q2: The tr operator works on single characters only, which means it won't work for double digit numbers. If you've only got single digits (as it sounds), it will work just fine. See perlop for details. If you can't figure out how it works, show us what you've tried and we'd be happy to give you a hand.

Q3: If the keys really are like your example (one upper case letter followed by one or more numbers), you're in luck - sort will do what you want right out of the box (just try it). If your data is a little different, fancier methods are needed (for example, A1 sorts before AA1, and A1 sorts before a1). If that's the case, the Schwartzian Transform could be applied after splitting the alpha and numeric parts (Super Search on this for more examples).

Update: Gah!! For some reason my brain wanted to over simplify the sort for Q3. A custom sort block, such as the ST or the one shown above is definitely needed. Thanks to GrandFather for thinking clearly when I was not.

Here's an example of how the ST could be used. The map-sort-map chain is processed from the bottom up, so start with the keys statement and read "backwards".

my @inkeys = map { # extract the original key name from the sorted # array refs, and return them in a list to @inkeys $_->[2]; } # sort the array refs: first sort alphabetically # (cmp) on the first element, then numerically # (<=>) on the second element sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] } # 'map' passes a list of array refs to 'sort' map { # split the key name into alpha and numeric parts my ( $alf, $num ) = $_ =~ m/([a-z]+)(\d+)/i; # create an array ref with the two parts of the # key that will be used for sorting, and the # original key name, return the array ref [ $alf, $num, $_ ]; } keys %{ $href };

Finally, don't forget about Super Search and the Tutorials section. They are both a tremendous resource for finding information around here.

Update: If the answers you got in Hash printing problem didn't help you, please be more specific.

Replies are listed 'Best First'.
Re^2: HOH again.. and tr/// and more..
by GrandFather (Saint) on Mar 07, 2006 at 03:09 UTC
    If the keys really are like your example (one upper case letter followed by one or more numbers), you're in luck - sort will do what you want right out of the box (just try it).

    Err, um, not quite:

    use strict; use warnings; my @array = qw(a1 a10 a100 a11 b2 b10 b200 b25); print 'bobf sort: ', join ' ', sort @array; print "\n GF sort: ", join ' ', sort alfNum @array; 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:

    bobf sort: a1 a10 a100 a11 b10 b2 b200 b25 GF sort: a1 a10 a11 a100 b2 b10 b25 b200

    DWIM is Perl's answer to Gödel