in reply to Re^2: Sorting a matrix based on the values of columns
in thread Sorting a matrix based on the values of columns
Hello raghuprasad241, I have a different approach for you. Previously, due to some hole in your input data that i have to go through all the trouble to make that big code. Now you have a much nicer input so we can make it look like this.
#!/usr/bin/perl use v5.10; use strict; use warnings; sub tranpose(@); my @row; my @sorted; while(my $line = <DATA>) { push @row, [split(/\s+/,$line)]; } @sorted = map { my $in = $_; [map {sprintf("%6s",$_)} @$in]; } #Reformat before prin +t tranpose #tranpose back to ori +ginal form map {[sort {$a <=> $b} @$_]} #sort each column tranpose @row; map {print @$_,"\n"} @sorted; sub tranpose(@) { my @out; foreach my $j (0..$#{$_[0]}) { push @out, [map {$_[$_][$j]} (0..$#_)]; } @out; } __DATA__ 1014 1 10 1015 51 100 1016 11 50 1017 101 999
Output:
1014 1 10 1015 11 50 1016 51 100 1017 101 999
Hope this help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Sorting a matrix based on the values of columns
by raghuprasad241 (Beadle) on Mar 18, 2016 at 14:53 UTC |