#! /usr/bin/perl -w
use strict;

# Sorting a matrix

my @mat = ([1,  2,  3, 4,  5],
	   [-3, 5, -6, 10, 7], 
	   [0, 22, -1, 11, 13],
	   [4, 10, 2,  44, 23]
	   );
PrintMat(\@mat);

# Sort using the first column
my @sMat = sort {$a->[1] <=> $b->[0]} @mat;
PrintMat(\@sMat);

# Sort using the third column
@sMat = sort {$a->[2] <=> $b->[2]} @mat;
PrintMat(\@sMat);


sub PrintMat {
    my ($matref) = @_;

    foreach my $row (@{$matref}) {
	for(my $i = 0; $i < @{$row}; ++$i) {
	    printf "%-3d ", $row->[$i];
	}
	print "\n";
    }
    print "\n";
}
	    
