use strict; use warnings; my %letters = ('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000); my $roman = "MCCMLXXIV"; #1874 my $result = 0; my $high_score = 0; foreach my $letter ( split(//, reverse($roman)) ) { my $score = $letters{$letter}; if ($score >= $high_score) { $result += $score; } else { $result -= $score; } $high_score = $score if $score > $high_score; } print "$result\n";