%letters = ('dny' => '#225', 'kh' => '#35', 'k' => '#12', 'h' => '#10', 'a' => '#8', 't' => '#7', 'o' => '#6', 's' => '#5'); $string = 'khatos'; #if you ever want to match more than 3 characters #then change the 3 below to whatever number #you want. while($string =~ s/([a-zA-Z]{1,3})/&roman($1)/e) {} print "$string\n"; sub roman { my $letter = shift; my $remainder; while(length($letter) > 0) { if(exists($letters{$letter})) { return $letters{$letter}.$remainder; } $remainder = chop($letter).$remainder; } #if the character isn't found in %letters it #will return '#?', if you return an alphabetic #character which isn't in %letters then the #program will loop forever. return '#?'; }