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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://243345]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-01-21 18:48 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (229 votes). Check out past polls.