Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

(dkubb) Re: (2) Idiom for looping thru key/value pairs

by dkubb (Deacon)
on May 27, 2001 at 07:19 UTC ( [id://83567]=note: print w/replies, xml ) Need Help??


in reply to Idiom for looping thru key/value pairs

The CPAN module Tie::DxHash will allow you to make a hash that retains the order the keys were entered, and allows duplicate keys as well.

Here's a short example to show you how simple it's usage is:

use Tie::DxHash; tie my %hash, 'Tie::DxHash'; %hash = ( a => 1, a => 2, b => 2, b => 3, );

An interesting thing to note is that in the assignment to %hash the duplicate keys are not clobbered.

You can then iterate over this hash using while/each or a foreach/keys, and the elements will be returned in the order they were inserted into the hash.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found