#!/usr/bin/perl -w

use strict;

my @lines;
while (<>) {
	die "Line $.: Invalid line\n" unless /^(\d{9})\s+(.*)\s+(\d+)$/;
	my ($num, $name, $mark) = ($1, $2, $3, $4);
	$name =~  s/(\w+) (\w+)/\U$2\E, $1/;
	push @lines, sprintf "%09d %-25s %3d\n", $num, $name, $mark;
}

print "Students sorted by number:\n";
for (sort @lines) {
	print;
}

print "\nStudents sorted by decreasing mark:\n";
for (sort { (split ' ', $b)[-1] <=> (split ' ', $a)[-1] } @lines) {
	print;
}
