The one-to-one correspondence of the %hash1 and %hash2 keys makes me a little suspicious, where is the point of having both hashes if that's the whole of the data? I'm going to hazard a guess that %hash2 is larger than %hash1, the latter being used to select a sample from the former. I call them %data and %pick in the script below which uses rand to generate the data (and srand so that it is consistent between runs).

use strict; use warnings; srand 12345; my %pick = map { $_ => 1 } 1 .. 5, 8, 12 .. 16, 19, 22, 25, 37 .. 41, 62, 75, 77; my @letters = ( q{a} .. q{e} ); my %data = map { $_ => { key => $letters[ rand @letters ] } } 1 .. 80; printf qq{key %2d and %s\n}, unpack q{xNXXXXXa} for sort map { pack q{aN}, $data{ $_ }->{ key }, 0 + $_ } keys %pick;

The output.

key 13 and a key 15 and a key 16 and a key 25 and a key 41 and a key 1 and b key 3 and b key 22 and b key 40 and b key 12 and c key 38 and c key 62 and c key 75 and c key 4 and d key 5 and d key 19 and d key 37 and d key 2 and e key 8 and e key 14 and e key 39 and e key 77 and e

I hope this guess bears some relation to the real problems and is helpful.

Update: A perhaps more readable alternative to the "skip one - read four - skip back five - read one" unpack template would be this:-

printf qq{key %2d and %s\n}, reverse unpack q{aN} for sort map { pack q{aN}, $data{ $_ }->{ key }, 0 + $_ } keys %pick;

Cheers,

JohnGG


In reply to Re: hash sort by johngg
in thread hash sort by Anonymous Monk

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.