If, let us say, this isn’t an “XY problem,” here are three important things you need to ponder:
-
Elements in a hash are accessed only by key. They have no “order.” But you can get a list of all of the keys in order with sort keys hashref.
-
The data structure that you describe is “an array of hashrefs,” where each of these contains two keys ... (say) key and value.
-
Behold the magic of references. Notice that I said “hashref,” which is Perl-speak for “a reference to a hash.” A reference is exactly what it sounds like: “a thing” that refers to another “thing.” If you examine a reference, you examine what it refers to. If you change a reference, you change the thing that it refers to. (You can also make the reference refer-to a different “thing,” which is an altogether different notion.) So the array that I spoke of in #2 contains a list of “individual things,” as all arrays do, but each one of those “individual things” is a reference to something much more interesting ... each one of which, in turn, can contain “references to” something else, ad infinitum. Every one of those references has the power, not only to retrieve the value that it refers to, but to alter it.
Chew on this for a while, and then start chewing on perldoc perlref. (Chew slowly, and avoid looking directly at the light-bulbs because they have a habit of turning “on” at the strangest moments.)