On Jun 8, Pedro A Reche Gallardo said:
>Hi all, I have a file with 20 columns of positive and negative decimal
>numbers (eg: 9.782 -8.983) separated by a black space, and everycolumn
>is marked on top with an single alphabet letter.
>A D C B
>9.782 -8.983 -3.483 -3.219
>0.995 -0.330 9.994 -4.000
>
>and, I would like to reorder them to look as follows.
>
>A B C D
>9.782 -3.219 -3.483 -8.893
>0.995 -4.000 9.994 -0.330
I take it you mean you have some columns, and their headers are not in
sorted alphabetical order, and you want them to be. A simple solution is
to sort the header row's INDICES, and use those values to print the data
in each row in the proper order.
#!/usr/bin/perl -w
use strict;
open IN, "< $ARGV[0]" or die "can't read $ARGV[0]: $!";
open OUT, "> $ARGV[0].new" or die "can't write $ARGV[0].new: $!";
# get header line
my @headers = split ' ', <IN>;
my @idx = sort { $headers[$a] cmp $headers[$b] } 0 .. $#headers;
# print header line in proper order
print OUT join("\t", @headers[@idx]), "\n";
# print data rows in proper order
print OUT join(" ", (split ' ')[@idx]), "\n" while <IN>;
close IN;
close OUT;
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **