perl:将许多数组元素替换为文件中的另一个对应数组元素



我试图用文件中的另一个相应的数组元素替换许多数组元素,但它的执行年龄为年龄。有更简单的方法吗?以下是我的代码:

open( my $in,  '<', "Test.txt")  or die "cannot open Test.txt $!";
open( my $out, '>', "TestFinal.txt") or die "cannot create TestFinal $!";
while( <$in>)
{ 
    for(my $i=2 ; $i<=$LastRowGlossary; $i++)
    {
        s/$variable[$i]/$vardescription[$i]/g;
    }
    for(my $j=2 ; $j<=$LastRowTable; $j++)
    {
        s/$COVERAGE_TYPE_CODE[$j]/$TCOVERAGE[$j]/g;
        s/$CVG_TEST_CRIT_CD[$j]/$TCVG_TEST_CRIT_TYP[$j]/g;
    }
    print {$out} $_;
}
close $in; 
close $out;

请建议。

有时,生成正则是有帮助的:

#!/usr/bin/perl
use warnings;
use strict;
my @variables =    qw/a b c d e f g h/;
my @descriptions = qw/A B C D E F G H/;
my %replace;
@replace{@variables} = @descriptions;
my $string = 'xaxbxcxdxexfxgxhx';
my $pattern = '(' . join('|', map quotemeta, @variables) . ')';
$string =~ s/$pattern/$replace{$1}/g;
print "$stringn";

相关内容

最新更新