in reply to hash ordered by an array

In general, it doesn't make sense to want a hash in a particular order.

Put another way, if you need things in a particular order, a hash is usually the wrong data structure. You can do it, as testimonied by the many modules that will do it for you, but if you explain why you think you need this, you will often get a better solution.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: hash ordered by an array
by baxy77bax (Deacon) on Jul 11, 2008 at 12:42 UTC
    i agree completely, if you would like to have particulary orderd structure of data it is better to use @arrays they have indexes that sorts your data in particular way , %hashes are special for their keys that are associated with some value. i mean, you could sort those keys like this:
    sort keys %hash;
    but to do it in a particular order you would somehow have to index those keys. top of the head solution;
    %hash = ('dog' => 1, 'big' => 5, 'jak' => 4, 'mack' => 19); @array = qw(dog mack jak big); foreach $val (@array){ print "$hash{$val}\n"; }
    in this way you are doing, in a particular order something with your %hash values. i suppose this isn't exactly what you had in mind but...