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

[FWP] Comparing two arrays



I'm not sure this is 'fun', but here goes: I've now for the second or 
third time in a few weeks needed to do a sort that compared several 
entries of an array.  I seem to end up with something that looks like:

    my @arrayofstuff ;
    push @arryofstuff, [ [ key1, key2, key3, key4, ....] , value ]

Then I want to sort that if there were a fixed number of 'key' values 
could do as:
 $a->[0][0] cmp $b->[0][0] or $a->[0][1] cmp $b->[0][1] or ...
[and then after the sort, a final:
    map { $_->[1] } [SORTEDLIST].

Anyhow, I've tried a bunch of things and the reason I'm asking here 
[rather than c.l.p.mod, or just not asking at all, since I have some code 
that works fine] is because everything I"ve tried ends up feeling like it 
was too much of a struggle [and UGLY].  is there some clever/fun/cute
way to do this kind of thing?

[I can't remember exactly, but I think the last time I did this I did 
some kind of a pop-loop on copies of the arrays:

   { my @a1 = @{$a->[0]}; my @a2 = @{$b->[0]}; my $res ;
     while (@a1 and @a2)
     {
        $res = (pop @a1) cmp (pop @a2) ;
        last if $res ;
     }
     $res = (@a1 <=> @a2) if ($res == 0);
     $res ;
   }

/Bernie\
-- 
Bernie Cosell                     Fantasy Farm Fibers
mailto:bernie@fantasyfarm.com     Pearisburg, VA
    -->  Too many people, too few sheep  <--          

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe