[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Complex sort(?)



> I have a page of links (http://www.mycoinfo.com/mycoinfolinks.html),
> which needs to be alphabetized two ways.  First, the various headings
> need to be in alphabetical order, and second, items within the headings
> need to be alphabetized.

Hi,

I made a neat sort like this for work.  I'm just learning perl, so I
probably did it in a lot more code than someone else would, but it works
beautifully.  I don't have the code in from of me, so I'll just describe
it to you.  I was working with data from an sql database, but you might
be able to adapt it.

I created a hash for wich the key was each heading and the value was a
pointer to an array of data under that heading.  I did a foreach on the
hash with a sort so they came out in order.  During that loop, I call a
subroutine which I call, "RealSmartSort".  I send it the array pointer
and created a hash for each element in the array for which the value and
the key were the same (this will work even if you have duplicates in
your array).  I then perform manipulations on each key.  I'm not sure if
I remember the order of manipulations, which is important, but I did it
like this.  I lower cased the data, matched every group of digits not
followed by a decimal point in a preparatory loop, added as many leading
zeroes as was needed to bring every number up to the largest number of
digits found in another loop, then simply did a "sort keys %hash" in a
foreach loop and assigned the values of the hash back to the array in
alpha-numeric order.

I thought it was pretty cool the way it worked out, but as I *am* a
beginner, I'm sure there's a better, more efficient, and easier way to
do it.  So the end result was a hash in the parent process keyed on
heading which I print out and then I print the values in the array that
was sorted by my subroutine.

Rob Leach
LANL

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org