in reply to Sort a 2D array based on 2 columns

This will do the trick in Perl:
use strict; use warnings; my @megaMotif = ( [ "AGCT", "0", "370", "1" ], [ "AGGT", "3", "52", "1" ], [ "TGAA", "2", "233", "0" ], [ "AGAG", "0", "32", "0" ] ); my @megaMotifSorted = sort { $a->[1] <=> $b->[1] || $a->[3] <=> $b->[3] } @megaMotif;
Please note the following two things:
  1. The name of the language is "Perl", not "PERL".
  2. When making your array, the outhernmost brackets should be parentheses "( ... )" and not square brackets "[ ... ]". Square brackets will make your list into an anonymous array rather than a normal array. In your version @megaMotif only contains one element, such as "ARRAY(some_hex_value)". Doing a sort on an array with only one element will not bring you very far.

    Did you just change the brackets in your posting or am I going mad? I could have sworn I just copied-and-pasted your code in my editor and the outernmost brackets were square and not round. If you did change them afterwards, make a little note of it otherwise it gets difficult to follow the discussion.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Sort a 2D array based on 2 columns
by masala_curry (Novice) on Apr 26, 2009 at 08:01 UTC
    Thank You.
    Sometimes I get stuck badly and dont see the easiest thing by which I could move ahead like this. A little push in the right direction is invaluable.
    Happens with anyone else?
    Thank You again!
      Sure does! Happens a lot to me. Here are some links to posts to remind you that you are in good company - the best really:

      Posting questions, even ones that turn out to be silly mistakes can be very, very helpful to the next person down the road who makes the same silly mistake. Perl Monks is the great place it is because people have had the courage to make mistakes and let others see them.

      Best, beth