5.7. Retrieving from a Hash in Insertion Order5.7.1. ProblemThe keys and each functions traverse the hash elements in a strange order, and you want them in the order in which you inserted them. 5.7.2. Solutionuse Tie::IxHash; tie %HASH, "Tie::IxHash"; # manipulate %HASH @keys = keys %HASH; # @keys is in insertion order 5.7.3. DiscussionTie::IxHash makes keys, each, and values return the hash elements in the order they were added. This often removes the need to preprocess the hash keys with a complex sort comparison or maintain a distinct array containing the keys in the order they were inserted into the hash. Tie::IxHash also provides an object-oriented interface to splice, push, pop, shift, unshift, keys, values, and delete, among others. Here's an example, showing both keys and each:
5.7.4. See AlsoThe documentation for the CPAN module Tie::IxHash; Recipe 13.5
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|