in reply to Re^2: beginner - ordering tuples
in thread beginner - ordering tuples

I modified the above as follows but it will probably make you perl programmers cringe with despair. Sorry rolf for desecrating your original answer :)
use strict; use warnings; $" = ', '; my @datas; #- parse input for (<DATA>){ my @entries = m/<(\d+),(\d+)>/; push @datas, [@entries]; } #- sort @datas=sort sort_test @datas; #- output use Data::Dumper; print Dumper \@datas; sub sort_test() { if ($a->[0] < $b->[0]) { return -1; }elsif ($a->[0] > $b->[0]){ return 1 }else { if ($a->[1] > $b->[1]) { return -1; } elsif ($a->[1] < $b->[1]) { return 1; } else { return 0; } } } __DATA__ <12,20> <1,5> <7,10> <7,12> <7,15>

Replies are listed 'Best First'.
Re^4: beginner - ordering tuples
by Anonymous Monk on Nov 13, 2010 at 13:36 UTC
    I've just noticed, why has it put some of the output in single quotes: (look at the first and last one). I did it with different data and the last 2 tuples had quotes around the second value.
    $VAR1 = [ [ 1, '5' ], [ 7, 15 ], [ 7, 12 ], [ 7, 10 ], [ 12, '20' ] ];
      why has it put some of the output in single quotes:

      The input array is initialised with strings. Left unmodified, all the values would be output in quotes.

      During the sort process, any values that are compared as numbers using <=>, will first be converted, internally, to their (binary) numeric representation.

      But as the first & last pairs in your example have unique first elements (1 & 12), there is never any need to compare their second elements, hence they are never converted to numeric representation, and so are output as strings.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      +