my %triplets = (AAA => 'K', AAG => 'K', GGA => 'G',); my $sequence = 'AAAGGAAAG'; $sequence =~ s/(.{3})/$triplets{$1}/g; print $sequence;