Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: hash sort

by johngg (Canon)
on May 31, 2017 at 17:45 UTC ( [id://1191743]=note: print w/replies, xml ) Need Help??


in reply to hash sort

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1191743]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found