The items from @allgs (which would appear to be numbers) will be sorted according to their associated value in the hash %g2o (compared as string), or, if equal, the smallest value (as a number) comes first.
For example:
%g2o = ( 128 => 'a',
13 => 'b',
34 => 'a',
28 => 'c' );
@allgs = (128, 13, 34 );
@allgsplane = sort { ($g2o{$a} cmp $g2o{$b}) || ($a <=> $b) } @allgs;
print "@allgsplane\n";
prints
34 128 13
All items with the value 'a' in the hash (34, 128) come before the item with value 'b' (13), because 'a' comes before 'b', but since there are two items with 'a', the smaller one (34) comes before the larger one (128).
Note that my hash contains more items than the list I'm sorting, but those will simply be ignored.
If the hash contains fewer items than the list, then it'll still work, with the missing items getting a default value of 0 (actually undef, but numerically, that is 0), but with "use of unitialized value" warnings.
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.