Perl 什么';插入多行替换字符串并将其应用于单个数组值的正确语法是什么?
这是一个场景——这个过程的一个步骤涉及到在数据明显拼写错误时修复城市名称,以及一些基本转换,如“MTN”到“Mountain”等等。我已经构建了一个包含多个替换字符串的变量,我正在尝试稍后在一个输入字段上应用这组subPerl 什么';插入多行替换字符串并将其应用于单个数组值的正确语法是什么?,perl,Perl,这是一个场景——这个过程的一个步骤涉及到在数据明显拼写错误时修复城市名称,以及一些基本转换,如“MTN”到“Mountain”等等。我已经构建了一个包含多个替换字符串的变量,我正在尝试稍后在一个输入字段上应用这组sub my $citysub = <<'EOF'; s/DEQUEEN/DE QUEEN/; s/ELDORADO/EL DORADO/; ... # there are about 100 such substitution strin
my $citysub = <<'EOF';
s/DEQUEEN/DE QUEEN/;
s/ELDORADO/EL DORADO/;
... # there are about 100 such substitution strings
EOF
...
while ($line <INFILE>)
{
...
@field = split(/","/,$line); # it's a comma-delimited file with quoted strings; this is spltting exactly like I intend; at the end, I'll piece it back together properly
...
# the 9th field and 12th field are city names, i.e., $field[8] and $field[12]
$field[8] =~ $citysub; # this is what I'm wanting to do, but it doesn't work!
# since that doesn't work, I'm using the following, but it's much slower, obviiously
$field[8] = `echo $field[8]|sed -e "$citysub"`; # external calls to system commands
my$citysub=
说明:创建“要匹配的对象”=>“要替换的对象”的散列。然后循环该散列并使用要匹配的对象和要替换的对象运行s//
my %citysub = ( "DEQUEEN" => "DE QUEEN", "ELDORADO" => "EL DORADO" );
for my $find ( keys %citysub ) {
my $replace = $citysub{ $find };
$field[8] =~ s/$find/$replace/g;
}