#!/usr/bin/perl use v5.10; use strict; use warnings; # This script will arrange the rows and columns of a matrix in the sorted order :-) my @row; while(my $line = ) { push @row, [split(/\s+/,$line)]; } foreach my $r1 (0 .. $#row) { #Take 1 row as base, check other rows my @remain = grep !/$r1/,(0 .. $#row); foreach my $r2 (0..$#remain) { # Now loop through the columns of each rows and compare when columns are same. foreach my $c1 (0..$#{$row[$r1]}) { foreach my $c2 (0..$#{$row[$r2]}) { if ($c1==$c2) # Make sure we are swapping values on the same column not across the columns if needed { if ($row[$r1][$c1] < $row[$r2][$c2]) { my $temp=$row[$r1][$c1]; $row[$r1][$c1]=$row[$r2][$c2]; $row[$r2][$c2]=$temp; } } } } } } map {print "@$_\n"} @row; #SUPPORT FUNCTION __DATA__ 1014 1 10 1015 51 100 1016 11 50 1017 101 999