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

Re^2: Sort hash with values

by 2teez (Priest)
on Jun 23, 2013 at 16:20 UTC ( #1040333=note: print w/ replies, xml ) Need Help??


in reply to Re: Sort hash with values
in thread Sort hash with values

Hi farang,
The mySort routine looks like it's not doing it's job properly or efficiently..
Though, I don't know how the OP wants his/her data sorted, but really there is no need for the mySort subroutine. Because, all that that subroutine is doing can be done in a sort block like so:

use strict; use warnings; my %IP_Store = ( "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" ); foreach my $Value ( sort { $IP_Store{$a} =~ /(\d+)/; my $firstVal = $1; $IP_Store{$b} =~ /(\d+)/; my $secVal = $1; $firstVal <=> $secVal; } keys %IP_Store ) { print "$IP_Store{$Value}\n"; print "System_ip = '$Value' \n"; }
Which will produce the same result....

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re^2: Sort hash with values
Download Code
Re^3: Sort hash with values
by Laurent_R (Vicar) on Jun 23, 2013 at 21:49 UTC
    Hi,

    I don't know how the OP wants his/her data sorted, but really there is no need for the mySort subroutine. Because, all that that subroutine is doing can be done in a sort block...

    Yes, you are right, but I guess that Farang wanted to offer a fix while changing as little as possible from the original code. It is often a dilemma for me (here and on other forums) when I see a clear coding error as well as a number of more or less clumsy or suboptimal things around: should we just give the bug fix to make the program work, toi solve the OP's immediate problem, or should we try to refactor the whole shebang. Sometimes, when I have time, I take the trouble of saying something like this: "your error is there, on this line of code, this is what you need to fix; but, BTW, I would think that it would also be better to (follow the following best practices | use a hash instead of an array (or an array instead of a hash) | work the algorithm the other way around | use the following (cleaner|more robust|faster) syntax | use strict and use warnings | use Perlish loops rather than C-style loops | use tr/// instead of s///g | use index instead of a regex | use map and grep instead of foreach (or foreach instead of map and grep) | whatever | etc.). But I don't always have time and I don't always do it.

      But I don't always have time and I don't always do it.

      And more often than not, advice like that is simply ignored -- being lazy is good, doesn't waste your time, doesn't annoy the OP :)

      Yeah, that's basically it. I hadn't looked closely at the mySort function until after I posted, because the sort results seemed reasonable. When I did look, it seemed awkward at best, so I made note of that in an update. I was confident other monks would address it much better than I could on the spur of the moment, and sure enough 2teez and AnomalousMonk have now done just that.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-08-01 02:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls