At 03:31 PM 2/5/2004 -0800, david wrote:
Shiping Wang wrote:

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

[snip]

i have never used Math::Matrix so i am not sure how it behave or works but
your code seems overly complicated for such a simple problem. even there
are unknown number of columns, as long as the first line (header) is there
to suggest the right order, you don't need to sort anything or do matrix
operation. for example, the following sorts the file and it doesn't need to
know how many columns to sort. you can have hundreds or thousands of
columns:

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

my $j = 0;

my %i = map{$_ => $j++} grep $_, split(/\D+/,my $line = <DATA>);

my @c = sort {$a <=> $b} keys %i;

#--
#-- header
#--
print join("\t",(split /\s+/,$line)[EMAIL PROTECTED]@c}]),"\n"; # Although I can guess this piece of code do the sorting then print out, but don't quite understand how it works, especially [EMAIL PROTECTED]@c}].

@c = (1 2 3 4 5 6)
and
@[EMAIL PROTECTED] = ( 0 3 5 1 2 4) -------- get all the values of %i ? Something like ($i{1} $i{2} ....${6})
then it likes
print join("\t", @[EMAIL PROTECTED]@c}]),"\n"; which become to: print join("\t", @_[ 0, 3, 5, 1, 2, 4]),"\n";


perl automatically sort array before printing?

Did I understand this right?

Thank you very much,

Shiping

#--
#-- the rest
#--
while(<DATA>){
        print join("\t",(split)[EMAIL PROTECTED]@c}]),"\n";
}

__DATA__
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

__END__

prints:

col1    col2    col3    col4    col5    col6
Abc     left    15.7    12.8    8       1
Def     top     19.7    13.8    9       0
gef     left    19.7    14.8    9       0
Dgf     right   99.6    12.3    9       4
cef     right   89.7    16.8    4       0
baf     bottom  79.8    32.8    7       5
efg     right   56.7    16.8    5       0
etg     left    34.7    12.8    2       7

david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)

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