--- Paul <[EMAIL PROTECTED]> wrote:
>
> --- David Michael <[EMAIL PROTECTED]> wrote:
> > Hello,
> > I have a hash that needs to be displayed in a certain order. I
> > tried
> >
> > foreach $key (sort (keys %HASH)) {
> > print $key;
> > }
> >
> > that sorts alphabetically. I need it in the order it was
> > inserted, so I made the value a number that increased for each key.
> I
> > need to sort by the value, but display the key. Any ideas ?
>
> Try flipping the hash.
>
> my %flipped;
> @flipped{values %HASH} = keys %HASH;
>
> That makes the keys of %HASH the values of %flipped, with the
> matching
> values of %HASH being the keys to which they are assigned.
> Now just do it the easy way.
>
> for my $key (sort keys %flipped) {
> print $flipped{$key};
> }
Another trick which would probably take less memory but more work is to
push each element onto an array as you insert it into the hash. Then
you preserve the order with the array, but you have more to keep track
of.
I don't like that solution, but someone might. =o)
=====
print "Just another Perl Hacker\n"; # edited for readability =o)
=============================================================
Real friends are those whom, when you inconvenience them, are bothered less by it than
you are. -- me. =o)
=============================================================
"There are trivial truths and there are great Truths.
The opposite of a trival truth is obviously false.
The opposite of a great Truth is also true." -- Neils Bohr
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/