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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |