Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: How to Sort a Hash According to the Keys in the Hash

by andal (Hermit)
on Dec 03, 2015 at 07:29 UTC ( #1149262=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to Sort a Hash According to the Keys in the Hash
in thread How to Sort a Hash According to the Keys in the Hash

No, you misunderstood him (or he misunderstood you :) sort is applied to the list of of keys returned by keys (the keys returns list in random order). So your foreach loop shall iterate over sorted list of keys. You then use current key to obtain value from hash. If you shall access your hash only ones, then you can use this approach. If there are multiple accesses, then it makes sense to keep sorted list of keys to avoid sorting multiple times.

On a side note. Your code in the original message contains error. You use variable $keyHoA as iterator of the loop, but to access value of the hash inside of the loop you use variable $req ($newHoA{$req}[0])

  • Comment on Re^3: How to Sort a Hash According to the Keys in the Hash

Replies are listed 'Best First'.
Re^4: How to Sort a Hash According to the Keys in the Hash
by AnomalousMonk (Archbishop) on Dec 03, 2015 at 18:36 UTC

    I'm not sure to whom this reply was intended to be addressed. I don't think it was to me.


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

      Hm. I was replying to comment about using "sort keys". Somehow I understood your words so, that you didn't think that this method is acceptable. Sorry.

        If ishootperls wants hash keys returned in the order in which the key/value pairs were inserted into the hash, then  sort keys %hash is not acceptable: it will return a list of the keys in lexically sorted order.


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-11-28 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?