Hello @redbull2012, Thank you for much for teaching me how to loop through each row and columns of the matrix and compare them. I took your code and modified it a little bit as follows. Let me know what you think about this or if you see any problems with the code ? Atleast during my testing it appears to be working fine.

#!/usr/bin/perl use v5.10; use strict; use warnings; # This script will arrange the rows and columns of a matrix in the sor +ted order :-) my @row; while(my $line = <DATA>) { 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 co +lumns are same. foreach my $c1 (0..$#{$row[$r1]}) { foreach my $c2 (0..$#{$row[$r2]}) { if ($c1==$c2) # Make sure we are swapping values o +n 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

In reply to Re^2: Sorting a matrix based on the values of columns by raghuprasad241
in thread Sorting a matrix based on the values of columns by raghuprasad241

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.