Hi David, thanks for your response.

My concern is that if the data has hundreds columns (which the order information I can get from another array or file) need to be rearranged, how can I do it.

Here is my test program, it seems working. Any suggestion or concern???

Shiping
_______________________________________________________________________________________________________________
#!/usr/bin/perl
# sortColumns.pl
use warnings;
use strict;
use Math::Matrix;
# use Data::Dumper;

my @rows;

while (my $line = <DATA>) {
  chomp($line);
  my @cols = split(/\s+/, $line);
  push @rows, [EMAIL PROTECTED];
}

foreach my $o (@rows) {
  print join "\t",@{$o},"\n";
}

    my $c = new Math::Matrix(@rows);
        $c -> print("Bring original data in matrix\nWhy not show chars???\n");
        my $tr = $c ->transpose;
        $tr -> print("Transposed data\n");

        my @trans = @{$tr};
        foreach my $t (@trans) {
                print join "\t",@{$t},"\n";
        }

        print "Sorted on 1st column:\n";
        @trans = sort {$a->[0] cmp $b->[0]} @trans;
        my $ret = new Math::Matrix(@trans);
        $ret -> print("sorted data\n");
        my $srt = $ret ->transpose;
        $srt -> print("back to A\n");
        my @srtrows = @{$srt};

foreach my $l (@srtrows) {
  print join "\t",@{$l},"\n";
}

print "It seems that final result gets what I want.\nAny suggestions???\n";

__DATA__
c2  c1  c4  c3  c6  c5
Abc 12.8 8 "left" 1 15.7
Def  13.8 9 "top" 0 19.7
gef  14.8 9 "left" 0 19.7
Dgf  12.3 9 "right" 4 99.6
cef  16.8 4 "right" 0 89.7
baf  32.8 7 "bottom" 5 79.8
efg  16.8 5 "right" 0 56.7
etg  12.8 2 "left" 7 34.7
__END__



At 11:06 AM 2/5/2004 -0800, david wrote:
Shiping Wang wrote:

> Hello,
>
> With this sample data set, I have a different question. How can I
> rearrange columns such as this:
>
> before:
> col1    col4    col5    col2    col6    col3
> Abc     12.8    8       left    1       15.7
> Def     13.8    9       top     0       19.7
> gef     14.8    9       left    0       19.7
> Dgf     12.3    9       right   4       99.6
> cef     16.8    4       right   0       89.7
> baf     32.8    7       bottom  5       79.8
> efg     16.8    5       right   0       56.7
> etg     12.8    2       left    7       34.7

[snip]

>
> I thought one way to do it is to transpose the array, sort _ColumnName_,
> then transpose it back. Is this right way to do it?

if the columns are always out in this order, you can easily reorder them
without sort:

#!/usr/bin/perl -w
use strict;

while(<DATA>){

        print join("\t",map {(split)[0,3,5,1,2,4]} $_),"\n";
}

__DATA__



-- 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