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.


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