Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Sort hash with values

by AnomalousMonk (Canon)
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 $_;  $_; }
    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( UEH1_system_ip UEH11_system_ip UEH25_system_ip 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'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040337]
[ww]: OTOH, -- for ads suggesting that being good at games is somehow eq "greatness"
[ambrus]: 1nickg: obviously the gamers don't know. but anyway, I need the whole room backlit, because watching a monitor in a dark room (like many people do) is tiring for my eyes, even more so than doing it in a lit room.
[1nickt]: ambrus I agree. I suspect one's eyes are always trying to adjust across the boundary between very bright and very dark ... like a camera's autofocus struggling to choose the target.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (15)
As of 2017-03-27 13:58 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.