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 > > after: > 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 > > 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?
Okay, it looks like you have two major pieces of data contained in this file. The first is the first line, which provides metadata. The other is the rest of the file, which provides the actual data, Egads! How did this file get in this shape to begin with? What you will have to do first is to create a mapping of position to target position, which can be done using the first line. You can then use the mapping to decide in what context to take the elements of each line. Greetings! E:\d_drive\perlStuff\giffy>perl -w my %column_index; my $header_line = <DATA>; my @column_names = split /\s+/, $header_line; my $position = 0; foreach my $column_name (@column_names) { $column_name =~ s/col//; $column_index{$column_name - 1} = $position++; } my @output_lines; my $line = <DATA>; chomp $line; while ($line) { my @elements = split /\s+/, $line; my @sorted_elements; push @sorted_elements, sprintf("%-10s", $elements[$column_index{$_}]) foreach sort {$a <=> $b} keys %column_index; push @output_lines, join(' ', @sorted_elements); $line = <DATA>; chomp $line if $line; } print "$_\n" foreach @output_lines; __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 ^Z 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 Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>