Greetings Monks,
I'm trying to sort a reasonably large amount of data stored in a hash, and I've
come up with code that works. However, I just don't like the code - it looks
like I'd curse the programmer that wrote it (me) if
I came across it.
I don't
think there's anything wrong with my algorithm, but I'd greatly
appreciate any comments on the style and maintainability of this code. More
idiomatic ways to write it (in particular the two lines concerning $idx and splice)
would also be welcomed.
What I'm doing should (hopefully) be fairly obvious from the code and comments,
but here's a general description anyway.
- For each part number, sort so that the order numbers are in order,
highest first
- For each part number, find (at most) the five highest order
numbers
- Now display all the part numbers, ordering them by the order
numbers
Note:
The data format isn't set in stone, and I'd be happy to change if a better alternative
exists.
The code:
#!/usr/bin/perl
use warnings;
use strict;
#example data below.
my %data = (
#Partno List of orders
"A1111" => [qw/12345 1234 123456 2345 233 23456/],
"B2222" => [qw/12345/],
"C3333" => [qw/12345 6789/],
"D4444" => [qw/12345 1234 6789/],
"E5555" => [qw/12345 1234 6789 99999 999999 444444 22222 111/]
+,
"F6666" => [qw/888888 9999999 6789/],
);
#Find the 5 numerically largest orders for each partno, and display th
+em.
foreach my $part_no (sort keys %data) {
my @matching_orders;
#Sort each order by number, largest first..
@matching_orders = sort { $a <=> $b } @{$data{$part_no}};
#We want upto the most recent five. Nasty hack.
my $idx = $#matching_orders-4;
$idx = 0 if($idx < 0);
my @top_five = reverse splice @matching_orders, $idx;
$data{$part_no} = \@top_five;
}
#I now know the first element of each part_no array is
#the largest order number that part_no appears on.
#sort by that element.
foreach my $part_no (sort { ${$data{$a}}[0] <=> ${$data{$b}}[0] } keys
+ %data) {
#Now display them
print "Part Number \"$part_no\" was found on: ";
print join ", ", @{$data{$part_no}};
print "\n";
}
Thanks in advance,
davis
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.