Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Save the resultant of " hash sorted by values "

by wind (Priest)
on Mar 23, 2011 at 20:14 UTC ( [id://895112]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Save the resultant of " hash sorted by values "
in thread Save the resultant of " hash sorted by values "

I feel your nitpicking, but fair enough. :) Yes, the point is that he's not sorting on the correct values (most likely) and he can certainly use a function to translate the keys just as well as using a ST. I personally would prefer to keep the code in one place given this is just an example though.

Also, your function doesn't actually work as is. Fixed:
sub ip_to_key { join '', map {sprintf "%03d", $_} split /\./, $_[0] }

Finally, thanks for sharing the pack method of doing a ST as it is definitely snazzier. I use it sometimes as well, but don't feel that it's as immediately decypherable by coders learning about sorts. Although, certainly the easiest method would probably be the functional translation sort {func($a) cmp func($b)}.

Replies are listed 'Best First'.
Re^4: Save the resultant of " hash sorted by values "
by ikegami (Patriarch) on Mar 23, 2011 at 22:27 UTC

    Yes, the point is that he's not sorting on the correct values (most likely) and he can certainly use a function to translate the keys just as well as using a ST.

    No. You must use the function (or equivalent). Whether ST is used or not has no bearing on whether it works or not.

    With STWithout ST
    With functionWorksWorks
    Without functionDoesn't workDoesn't works

    As an aside, using ST doesn't only make the code more complex, I suspect it also makes the makes it slower in this case.

      I feel you're reading more into my words than is there. If you'd like me to be more clear

      Yes, the point is that he's not sorting on the correct values (most likely) and he can certainly use a function to translate the keys within the sort block just as well as using a ST to precache the correctly translated keys.

      Didn't talk at all about what's a function and what isn't one. My main point is and was only that his sort order was probably not what he wanted. The means of fixing it is up to him, I just provided a single example.

      Nevertheless, thanks again for sharing alternative methods.

      Cheers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found