Hello,
I was advised by a member of Perl Guru to ask at Perl Monks whether it would be possible or not to perform a type of sort i'm trying to achieve. I would rather not perform this sort outside of the sort block, as I will later choose the type of sort from a hash lookup of subroutine refs.
Here is my code:
foreach my $colour (
sort {
if ($products{'Cat'}{'Pro'}{$a}{'Qua'} <= 0) {return 1;}
elsif ($products{'Cat'}{'Pro'}{$b}{'Qua'} <= 0) {return -1;}
}
sort {$a <=> $b || $a cmp $b}
keys %{$products{'Cat'}{'Pro'}}
) {
Here is its output (example):
Colour Quantity
70 (Grey brown mix) 3
72 (Green mix) 10
74 (Fuschia mix) 8
76 (Mauve mix) 11
77 (Blue mix) 6
90 (Beige pink mix) 12
91 (Peach cream mix) 8
92 (Silver cream mix) 10
93 (Beige blue mix) 9
78 (Black white mix) 0
75 (Red mix) 0
73 (Aqua mix) 0
71 (Beige mix) 0
As you can see, colours with a quantity of more than 0 are displayed above those with a quantity of 0. However, each group must then be sorted in alphabetical order, and although close, products with a quantity of 0 are being undesirably sorted in reverse.
I can see why the code doesn't work as desired, but i'm unable see a way to adjust it to suit my full requirements.
Is it possible without breaking out of the sort block?
Chris
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.