Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Ordering return from fetchrow_hashrefby gmax (Abbot) |
on Mar 02, 2004 at 17:18 UTC ( [id://333308]=note: print w/replies, xml ) | Need Help?? |
tye has shown you one way. However, let me question the reasons for wanting what you are asking Hashes are data structures where you can access elements by name rather than by position, as you do with arrays. Therefore a sorted hash is a contradiction. (Although Perl Tie mechanism can change something) Now, I see two reasons for wanting the result from a query in a hash:
In the first case, you don't really need a sorted hash, since you are using the hash names hardcoded in your application, so that the application itself will set the order. For example
If you need to process your columns according to a list of column names, here is a slight variation on tye's answer:
If you just want to use the column names together with their values, then you can use two different methods, according to your needs:
For more ideas, tips, and caveats on the same subject, see DBI Recipes. Update
Notice the empty hashref passed as argument. _ _ _ _ (_|| | |(_|>< _|
In Section
Seekers of Perl Wisdom
|
|