tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

Q1. I have a %HOH and I would like to sort it numericaly on the internal keys how do I do? Would you please help me with this? This problem has been houting me for a couple of weeks now..

Q2. Suppose I would like I to translitterate only the first digit in a collection of strings to a letter so that if the first letter is 1 will be 'A', two will be 'B' and so on.. Is it possible to do this somehow by the tr///-function or do I have to use substitution-function?

Q3. I have a hash with keys, which consist of a numeric part and a digit part. So that the keys have names like 'A1', 'A2' 'A11', 'B3', 'C22' 'D36' ect. I would like to sort this keys so that they are first sorted by the alphabeticaly by the alpabetic part, and then in the collection of each letter, numericaly by the numerical part. I have solved this by changing all 'A' to 1, all B to 2, ..and so on.. sorting this result numericaly, and changing back the first digits back to the letters.. Is there a better way? How would you do yourself?

Replies are listed 'Best First'.
Re: HOH again.. and tr/// and more..
by GrandFather (Saint) on Mar 07, 2006 at 02:17 UTC

    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.

    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
      Thank you very much for your answer Im sure it will help me very much. But I would also like to know a simpler thing: Just how to sort an internal hash of an HOH numericaly, the beta-keys or whatever one should call them. I have looked at some examples but, not found what Im looking for, maybee it is so easy nobody cares to make examples of it.

        As phrased that is a meaningless question. You can access a hash in sorted order by iterating through a sorted list of keys, but the hash itself is unsorted - that's the nature of the beast.

        You can sort an array because each element in the array has a defined position - it's index. But generally that is unimportant for a hash. There are tied hash implementations that allow itteration over the hash keys in sorted order and that may be what you want, but it may not either. At the end of the day the answer depends implicitley on what you are trying to achieve so there is no one answer to "how do I sort a hash?".


        DWIM is Perl's answer to Gödel
Re: HOH again.. and tr/// and more..
by bobf (Monsignor) on Mar 07, 2006 at 02:45 UTC

    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.

      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
Re: HOH again.. and tr/// and more..
by graff (Chancellor) on Mar 07, 2006 at 02:40 UTC
    Re Q1: If you give a small sample of relevant data in the hash structure, how you would like the sorting to come out, and a snippet of the code you've tried, help would come more easily. As it is, I'm not sure what you mean -- "sorting numerically on the internal keys" could mean different things, depending on the data and what you really want.

    Q2: you want a regex substitution, like  s/^(\d)/chr(ord($1)+0x10))/e

    Q3:

    my @sorted_keys = map { s/_0*//; $_ } sort map { s/(\d+)/sprintf("_%08d",$1)/e; $_ } keys %hash

    (that's the Schwartzian Transform approach, which might be more efficient, whereas the method that Grandfather suggested is easier to grasp)