Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

Re: Sort hash with values

by AnomalousMonk (Monsignor)
on Jun 23, 2013 at 20:03 UTC ( #1040337=note: print w/ replies, xml ) Need Help??


in reply to Sort hash with values

I, too, do not understand very well what Rahul Gupta ultimately wants to achieve, but insofar as a numeric-ascending sort of the values of a hash according to a sub-string of decimal digits within each value is concerned, a number of useful answers have been given.

A couple of intersting approaches have been touched on briefly or not at all: our old, work-horse friend the Schwartzian Transform (ST) exemplified by 2teez, and our exciting new friend the Guttman-Rosler Transform (GRT). Both of these techniques are thoroughly discussed in A Fresh Look at Efficient Perl Sorting. The advantage of GRT is that it uses no explicit sort-block at all, but depends on the default, hence fastest, lexicographic-ascending sort ordering of sort. (Update: Well, I'm assuming the implicit sort code has been optimized up the wazoo, so nothing you could do in an explicit block, with its inherent call overhead, could be faster.)

In the following examples, the
    map { dd $_;  $_; }
and
    map { print qq{=$_=};  $_; }
expressions are intended to show what is being fed to the sort built-in. Do not include them in production code.

>perl -wMstrict -le "use Data::Dump; ;; my %IP_Store = qw( 11.0.0.1 UEH1_system_ip 11.0.0.11 UEH11_system_ip 11.0.0.3 UEH25_system_ip 11.0.0.25 UEH111_system_ip ); ;; ;; print 'Schwartzian Transform'; my @sorted1 = map $_->[0], sort { $a->[1] <=> $b->[1] } map { dd $_; $_; } map [ $_, /\d+/g ], values %IP_Store ; print qq{'$_'} for @sorted1; ;; ;; print 'Guttman-Rosler Transform'; my $n = 10; my $fmt = qq{%0${n}d}; my $unp = qq{x$n a*}; ;; my @sorted2 = map unpack($unp, $_), sort map { print qq{=$_=}; $_; } map sprintf($fmt, /\d+/g) . $_, values %IP_Store ; print qq{'$_'} for @sorted2; " Schwartzian Transform ["UEH25_system_ip", 25] ["UEH1_system_ip", 1] ["UEH11_system_ip", 11] ["UEH111_system_ip", 111] 'UEH1_system_ip' 'UEH11_system_ip' 'UEH25_system_ip' 'UEH111_system_ip' Guttman-Rosler Transform =0000000025UEH25_system_ip= =0000000001UEH1_system_ip= =0000000011UEH11_system_ip= =0000000111UEH111_system_ip= 'UEH1_system_ip' 'UEH11_system_ip' 'UEH25_system_ip' 'UEH111_system_ip'


Comment on Re: Sort hash with values
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-04-19 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls