#!/usr/bin/perl -w
use strict;
## get first line with col names
my $headers = <>;
## store column names into array
my @column_names = split /\s+/, $headers;
## print out first line (sorted column names)
print join(' ', sort @column_names), "\n";
## The hash %column_order will contain column names as keys, and position of
column in original file as a hash value
my %column_order = map(($column_names[$_] => $_), 0..$#column_names);
## process rest of file:
while (<>) {
## skip blank lines
next if /^\s*$/;
## cut newline
chomp;
## split columns into array
my @vals = split /\s+/;
print join(' ', ## join fields with space...
## for each column name, get it's position (it's $column_order{$_}),
## and obtain appropriate value from @vals;
map $vals[$column_order{$_}],
## the line below will feed sorted col names to map()
sort keys %column_order
), "\n";
}
On Friday 08 June 2001 16:20, Pedro A Reche Gallardo wrote:
> 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.
> This is an example.
>
> 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
>
> Any help, or suggestion will be welcomed.
> Cheers
> ***************************************************************************
> PEDRO a. RECHE gallardo, pHD TL: 617 632
> 3824
> Scientist, Mol.Immnunol.Foundation, FX: 617 632 3351
> Dana-Farber Cancer Institute, EM:
> [EMAIL PROTECTED]
> Harvard Medical School, URL: http://www.reche.org
> 44 Binney Street, D610C,
> Boston, MA 02115
> ***************************************************************************
--
Ondrej Par
Internet Securities
Software Engineer
e-mail: [EMAIL PROTECTED]
Phone: +420 2 222 543 45 ext. 112