#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper qw(Dumper);

my %grades;
$grades{"Foo"}{1} = 97;
$grades{"Foo"}{2} = 107;
$grades{"Peti"}{1} = 88;
$grades{"Peti"}{3} = 89;
$grades{"Peti"}{4} = 99;

print Dumper \%grades;
print "----------------\n";

for my $name (sort keys %grades) {
    print "For $name:\n";

    my @subjects = sort keys %{ $grades{$name} };

    my $counter = 0;
    my $most_recent_subject;
    my $most_recent_grade;

    for my $subject (@subjects) {
        $counter++;

        if ($counter == 1) {
            print "line $counter: no action\n";
        }
        else {
            my $grade = $grades{$name}{$subject};

            my $subject_result = $subject - $most_recent_subject;
            my $grade_result = $grade - $most_recent_grade;

            print "line $counter: subject $subject_result, $grade_result\n";
        }

        $most_recent_subject = $subject;
        $most_recent_grade = $grades{$name}{$subject};
    }
}
