Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Printing a hash in a specific order?

by dga (Hermit)
on Mar 15, 2003 at 20:53 UTC ( #243345=note: print w/ replies, xml ) Need Help??


in reply to Printing a hash in a specific order?

Something which has not been explicitly said, but which was alluded to in the Template post goes like this.

my @keys_in_order = qw( key type flags Alias-2 ... ); #declared someplace so it doesn't get assigned each pass sub Print { my $hashref=shift; foreach my $k ( @keys_in_order ) { my $value=$hashref->{$k} if defined $hashref->{$k}; #rest of processing here } }

This way the keys come out in order if something defined was put in that spot in the hash. (this could be changed to just if or if exists depending on what values are really there and which aren't, decided by the application paramaters. This allows values to have 0 and still print in the current incarnation.) So changing the key order printout is as easy as changing the array which says which order to print out the hash values. You get the convienience of a hash which remains a fully optimal hash until printed then prints out in an arbritrary order for a minor cost.


Comment on Re: Printing a hash in a specific order?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://243345]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-09-02 11:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls