Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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'

In reply to Re: Sort hash with values by AnomalousMonk
in thread Sort hash with values by Rahul Gupta

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (6)
    As of 2014-04-21 02:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (489 votes), past polls