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

Re: [MacPerl] using one array to sort another



On Mon, Jun 28, 1999 at 07:25:20AM -0400, cetasix@pop.erols.com wrote:
> I have an array @comp_array which is sorted in the following manner:
> 
> 1
> 1.1
> 1.1.p.1
> 1.1.1
> 1.1.2
> 1.1.3
> 
> I would like to use the above arrangement to sort the following associative
> array called @names

Hmm, you can't have an associative array (actually, 'hash' is the preferred
term now) called @names.  An associate array would be called %names.
@names would be a regular array.

> 1.1.2|||Jeff||Alexander
> 1|||Joe||Alexander
> 1.1.p.1|||Claudia||Alexander
> 1.1|||Nathan||Alexander
> 1.1.3|||Herb||Alexander
> 1.1.1|||Bob||Alexander
> 
> The end result I would like to have is an array @results to be:
> 
> 1|||Joe||Alexander
> 1.1|||Nathan||Alexander
> 1.1.p.1|||Claudia||Alexander
> 1.1.1|||Bob||Alexander
> 1.1.2|||Jeff||Alexander
> 1.1.3|||Herb||Alexander
> 
> Any ideas?

I was going to suggest doing it opposite to how Arved suggested:
Create a hash of the sorted array, with the key being the element and the
value being the position, and then do a sort by looking up the position in
the hash.  But, as long as all the elements you want from @names have a
corresponding entry in @comp_array, then Arved's solution makes a lot more
sense.

Ronald

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