intoperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I need my hash table to printed out nicely and "SORTED". But looks like i am missing someting, Pls see the code below and the output :

Code : foreach(sort keys %SRV) { print"$_: "."$SRV{$_}"; } OutPut: Enter the Serial No. against the server you want to check CPU for: + 1 zumen530n.pdg.duranto.com 10 zumen546n.pdg.duranto.com 11 zumen547n.pdg.duranto.com 12 zumen548n.pdg.duranto.com 13 zumen549n.pdg.duranto.com 14 zumen550n.pdg.duranto.com 15 zumen551n.pdg.duranto.com 16 zumen552n.pdg.duranto.com 17 zumen553n.pdg.duranto.com 18 zumen21n.pdg.duranto.com 19 zumen22n.pdg.duranto.com 2 zumen531n.pdg.duranto.com 20 zumen23n.pdg.duranto.com 21 zumen24n.pdg.duranto.com 3 zumen532n.pdg.duranto.com 4 zumen533n.pdg.duranto.com 5 zumen540n.pdg.duranto.com 6 zumen541n.pdg.duranto.com 7 zumen542n.pdg.duranto.com 8 zumen543n.pdg.duranto.com 9 zumen544n.pdg.duranto.com

Replies are listed 'Best First'.
Re: sorted Hash
by vinoth.ree (Monsignor) on Aug 22, 2015 at 15:58 UTC

    You need numerical sort, try the following

    for $key ( sort {$a<=>$b} keys %hash) { print "($key)->($hash{$key})\n"; }

    In your way with foreach

    foreach (sort { $a <=> $b } keys(%SRV) ) { print"$_: "."$SRV{$_}"; }
    Update:

    foreach code added


    All is well. I learn by answering your questions...

      Thanks Vinoth, that was quick and my silly thing resolved. Everyone is so awesome here..

        Do you understand why that worked? If not, have you looked it up in the documentation?

        The way forward always starts with a minimal test.
Re: sorted Hash
by GotToBTru (Prior) on Aug 23, 2015 at 03:56 UTC

      The following link is indirectly accessible via the link given above, but it's a bit deeply buried there and I want to bring it above the fold because it's such a good article: A Fresh Look at Efficient Perl Sorting.


      Give a man a fish:  <%-{-{-{-<

Re: sorted Hash
by Not_a_Number (Prior) on Aug 23, 2015 at 17:03 UTC

    I don't know how you populate your hash, but you wouldn't need to sort at all if you used an array instead:

    my @SRV = ( 'zumen530n.pdg.duranto.com', 'zumen531n.pdg.duranto.com', 'zumen532n.pdg.duranto.com', # etc ); print "$_ $SRV[ $_ ]\n" for 0 .. $#SRV;

    Of course, this prints 0-20 instead of 1-21, but that's easy to fix if it's important.

    Update: In fact, whenever I see a hash like:

    my %h = ( 1 => 'foo', 2 => 'bar', 3 => 'baz', 4 => 'quux', );

    I always consider whether it wouldn't be better to use a simple array.

Re: sorted Hash
by locked_user sundialsvc4 (Abbot) on Aug 23, 2015 at 13:36 UTC

    Hey, let’s just make things a little easier for the next Gentle Reader, and actually explain things:

    In the statement:   sort { $a <=> $b } keys %hash)

    ... the part in bold underline is the Magick:   an in-line sort comparison subroutine (which always takes two arguments/variables, always named $a and $b).   This is used here in order that the numeric comparison operator (<=>) can be specified, instead of sort’s default, which is to use the string comparison operator (cmp).

    If you glance at the output in the original post, you will see that the sort keys are “sorted correctly” ... as strings, not as numbers.   (The “default” sorting behavior, in other words, is equivalent to:   sort { $a cmp $b } ...)

    Q.E.D.

    P.S.   This is one of the counter-intuitive features of Perl.   Ordinarily, you might expect that there would be one comparison-operator which would “know” whether the values being passed are numeric or string.   (As the ordinary “less-than, greater-than” relational operators do.)   But this, actually, is a feature of the language that is specifically designed for use in sorting.   The language, by default, consistently treats its sort-keys as strings, no matter what is their internal data-type at the moment, and yet makes it very easy ... as just demonstrated ... to compare them numerically.)