Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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]
[atcroft]: PriNet: Those code bits worked for me, with test hashes, but I didn't test it with more than a few simple test cases. *shrug* Had hoped it would help, at least.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2017-06-28 03:04 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (619 votes). Check out past polls.