Guruguhan N wrote:
>
> I have an array that is build like this
> foreach $i ( 0 .. @array1-1) {
> foreach $j ( 0 .. @array2-1) {
> $array3[$i][$j] = $array2[$j];
> }
> }
> The array3 has "m" rows and "n" columns of data.
>
> This code is written by some one else and I am trying to get the statistics
> for each of the column data stored in array3. Before getting the statistics, I
> wanted to sort each column data of the array3 in ascending order. I tried
> doing it as below, but could not succeed.
>
> foreach $i ( 0 .. @array1-1) {
> foreach $j ( 0 .. @array2-1) {
> @array3 = sort {$a <=> $b } @array3;
> }
> }
>
>
> Can some one tell me how do I this?

Hi Guruguhan.

Each element of @array3 is a /reference/ to an array. That's how Perl builds
its multi-dimensional arrays. Which dimension is 'columns' and which is 'rows'
is unclear, but by far the simplest case is if you want to sort each 'second-
level' array - @{$array3[0]}, @{$array3[1]}, @{$array3[2]} etc.

This code will do it.

  @$_ = sort { $a <=> $b } @$_ foreach @array3;

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to