有没有一种有效的方法,可以使用 Perl 哈希表中的值来替换多个字符串?
例如:
例如:
$regex{foo} = "bar";
$regex{hello} = "world";
$regex{python} = "perl";
open(F, "myfile.txt");
while (<F>) {
foreach $key (keys %regex) {
s/$key/$regex{$key}/g;
}
}
close(F);
在Perl中有没有一种方法可以实现上述功能?
eval
(http://perldoc.perl.org/functions/eval.html) - Nickeval
允许您在字符串内扩展变量,然后将该字符串作为Perl代码执行,例如:eval "s/$key/$regex{$key}/g" - Nicks///
已经插值了。 - ikegamieval
。 - Nick