我从Python转到Perl世界,想知道是否有一种简单的方法来将翻译或替换限制在短语中的一个词上?
在这个例子中,第二个单词kind也被改成了lind。是否有一种简单的方法可以进行翻译而不需要涉及循环?谢谢。
第一个单词已经被正确翻译为gazelle,但是第二个单词也被更改了,如您所见。
my $string = 'gazekke is one kind of antelope';
my $count = ($string =~ tr/k/l/);
print "There are $count changes \n";
print $string; # gazelle is one lind of antelope <-- kind becomes lind too!