What you need to do is to put the numbers into a structure that can be sorted while keeping track of the positions of the values in the original 2D array.

A hash is ideal for this, store the array indexes as an anonymous array in the hash value. Then sort on the hash key and that will give you the rank.

This was my first attempt:

use warnings; use strict; my @total; $total[1][1] = 123; $total[2][1] = 345; $total[3][1] = 222; # hash of arrays: hash{ number } = [ i, j ] my %hash; for my $i ( 1 .. $#total ) { for my $j ( 1 .. $#{$total[$i]} ) { $hash{ $total[$i][$j] } = [ $i, $j ]; } } my @rank; my $ranking = 0; foreach my $value ( sort keys %hash ){ my ( $i, $j ) = @{$hash{ $value }}; $rank[$i][$j] = ++$ranking; } for my $i ( 1 .. $#rank ) { for my $j ( 1 .. $#{$rank[$i]} ) { print "rank[$i][$j] is $rank[$i][$j]\n"; } }
and here is the output:

rank[1][1] is 1 rank[2][1] is 3 rank[3][1] is 2
But then I realised that this won't work if a value appears more than once: the hash element is overwritten. We need a little more intelligence, here's my second attempt:
use warnings; use strict; my @total; $total[1][1] = 123; $total[2][1] = 345; $total[3][1] = 222; $total[4][1] = 222; # hash of arrays: hash{ number } = [ i, j ] my %hash; for my $i ( 1 .. $#total ) { for my $j ( 1 .. $#{$total[$i]} ) { push @{$hash{ $total[$i][$j] }}, $i, $j; } } my @rank; my $ranking = 0; foreach my $value ( sort keys %hash ){ ++$ranking; while ( my ($i, $j ) = splice @{$hash{ $value }}, 0, 2 ){ $rank[$i][$j] = $ranking; } } for my $i ( 1 .. $#rank ) { for my $j ( 1 .. $#{$rank[$i]} ) { print "rank[$i][$j] is $rank[$i][$j]\n"; } }
which gives:
rank[1][1] is 1 rank[2][1] is 3 rank[3][1] is 2 rank[4][1] is 2

As an aside, you are the second poster today I have seen starting arrays at 1 - doing this is a bit confusing, and also you still get $array[0] created automatically and set to undef. This then gives you extra values if you do something like print @array.

-- iakobski


In reply to Re: I need to sort a 2D list. by iakobski
in thread I need to sort a 2D list. by aykes

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.