Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2020-12-04 20:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How often do you use taint mode?





      Results (62 votes). Check out past polls.

      Notices?