Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

First off, I'm not sure what order you expect your output to be. I'm going to guess that you want something like this:

.4 .6 .1 .3 .5 .1 .0 .2 .1 .5 .7 .1
In which case, I would create an array with the order of keys you want to go through:
my @keys = qw(start stop step); for my $type (@keys) { print $point->{$type}, "\t"; }
If some of the keys may not appear, you could skip them:
my @keys = qw(start stop step); for my $type (@keys) { next unless exists $point->{$type}; print $point->{$type}, "\t"; }
Or, in the spirit of TMTOWTDI, you could have a hash of weightings:
my %weights = ( start => 10, stop => 20, step => 30 ); for my $type ( sort { $weights{$a} <=> $weights{$b} } keys %$point ) { print $point->{$type}, "\t"; }
But I like the array better. No actual sorting required. The hash weights are more useful when you have great numbers of possible keys to sort, but only a few keys will be in any given hash. In this case, you may not want to generate the weights by hand, but generate them in code:
my %weights = do { my $i = 0; map { $_ => $i++ } qw(start stop step ot +her stuff in order here) };
Hope that helps.


In reply to Re: sorting a hash by keys, according to preference by Tanktalus
in thread sorting a hash by keys, according to preference by vindaloo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found