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

Hi, i need to write a code to arrange three arrays where every index location corresponds to each other in a manner that the first array is arranged alphabetically,and the seconded array should be arranged in a desecending order of size, and third array should take the corresponding values of the first two arrays.

Array1= {A,B,A,C,D,C,D,B} Array2={10,13,11,9,7,6,4,2} Array3={as,ad,ae,at,ag,ak,ao,we} The output i need is Array1={A,A,B,B,C,C,D,D} Array2={11,10,13,2,9,6,7,4} Array3={ae,as,ad,we,at,ak,ag,ao}

Replies are listed 'Best First'.
Re: Array sorting
by jwkrahn (Abbot) on Jul 27, 2011 at 06:20 UTC
    $ perl -le' my @Array1 = qw{ A B A C D C D B }; my @Array2 = qw{ 10 13 11 9 7 6 4 2 }; my @Array3 = qw{ as ad ae at ag ak ao we }; print "\@Array1 = @Array1\n\@Array2 = @Array2\n\@Array3 = @Array3\n"; my @order = sort { $Array1[$a] cmp $Array1[$b] || $Array2[$b] <=> $Arr +ay2[$a] } 0 .. $#Array1; print "\@Array1 = @Array1[@order]\n\@Array2 = @Array2[@order]\n\@Array +3 = @Array3[@order]"; ' @Array1 = A B A C D C D B @Array2 = 10 13 11 9 7 6 4 2 @Array3 = as ad ae at ag ak ao we @Array1 = A A B B C C D D @Array2 = 11 10 13 2 9 6 7 4 @Array3 = ae as ad we at ak ag ao
Re: Array sorting
by BrowserUk (Patriarch) on Jul 27, 2011 at 05:52 UTC

    (Assuming I've understood your rather muddled description?) Sort and array of indices to the first array in the appropriate order, and then use that to order the other two arrays:

    print @a1, @a2, @a3;; A B A C D C D B 10 13 11 9 7 6 4 2 as ad ae at ag ak ao we @order = sort{ $a1[ $a ] cmp $a1[ $b ] } 0 .. $#a1;; print @a1[ @order ];; A A B B C C D D print @a2[ @order ];; 10 11 13 2 9 6 7 4 print @a3[ @order ];; as ae ad we at ak ag ao

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.