http://www.perlmonks.org?node_id=614734

fiddler42 has asked for the wisdom of the Perl Monks concerning the following question:

Kind, Gentle Folk,

I have a need to sort a bunch of geometric boxes in two ways, and I'm not sure how to do it in perl efficiently. Here are the starting conditions of this problem:-

1. I have a text string (the name of a box)

2. I have the lower left coordinates of a box

3. I have the upper right coordinates of a box

4. I have the area of a box

I have several million overlapping boxes within the area of one large, master box. Here is what I need to do:-

1. Sort all of the areas, from largest to smallest

2. Once the sort completes, I need the N largest boxes that do not overlap at all.

I am not looking for a detailed solution, just a rough idea on the best approach. Can someone help fill in these blanks?

%NetAreaHash = (); $LL = join (" ",$MinX,$MinY); #$MinX and $MinY are decimals $UR = join (" ",$MaxX,$MaxY); #$MaxX and $MaxY are decimals $Area = abs($MaxX - $MinX) * abs($MaxY - $MinY); $NetAreaHash{$Area} = join (" ",$NetName,$LL,$UR,$Area); #$NetName is +a string #how do I numerically sort $NetAreaHash by $Area? #how do I take the sorted $NetAreaHash and identify the N largest area +s that do not overlap? N will usually be 2, 4 or 8.

Again, I'm just looking for some guidance here. The numerical sort of the hash probably won't be that tough, but identifying the N largest, non-overlapping boxes has me stumped at the moment!

Thanks,

Fiddler42