替换 $variable 改变 while 循环中的每次迭代
Replace $variable which changes every iteration in while loop
我有两个输入文件:filea
和 fileb
。在filea
中,我有一个字符串$pc_count$
,在fileb
中,有数字和字符串。我必须在 fileb
中搜索数字并在 filea
中替换 $pc_count$
。
我试过下面的代码,但是 fileb
的第一个值每次都重复 ($pc_count$
)。
open (IN_FILE, "<filea") or die "Please provide an correct file path\n";
open (IN_FILE1, "<fileb") or die "Please provide an correct file path\n";
my $i = 0;
while (<IN_FILE>) {
if($_ =~ /$pc_count$/) {
my $line = $_;
while (<IN_FILE1>) {
if($_ =~ /([a-z0-9-]+)[\s]+/) {
my $first = ;
$line =~ s/$pc_count$/$first/;
#print OUT_FILE("$line");
print $line;
}
}
}
#print OUT_FILE("$_");
}
close IN_FILE;
close IN_FILE1;
filea
case(pc)
'h$pc_count$ : $display("checkdata string %s ",pc_string);
endcase
fileb
fe0000
fe0001
fe0002
fe0003
..
..
..
结果:
case(pc)
'hfe000 : $display("checkdata string %s ",pc_string);
'hfe000 : $display("checkdata string %s ",pc_string);
'hfe000 : $display("checkdata string %s ",pc_string);
'hfe000 : $display("checkdata string %s ",pc_string);
..
..
..
..
endcase
第一次替换后,$line
发生了变化,不再有字符串 $pc_count$
。解决此问题的一种方法是在另一个变量中保留原始行的副本 ($line2
):
use warnings;
use strict;
open (IN_FILE, "<filea") or die "Please provide an correct file path\n";
open (IN_FILE1, "<fileb") or die "Please provide an correct file path\n";
my $i = 0;
while (<IN_FILE>) {
if($_ =~ /$pc_count$/) {
my $line = $_;
while (<IN_FILE1>) {
my $line2 = $line;
if($_ =~ /([a-z0-9-]+)[\s]+/) {
my $first = ;
$line2 =~ s/$pc_count$/$first/;
print $line2;
}
}
}
#print OUT_FILE("$_");
}
close IN_FILE;
close IN_FILE1;
输出:
'hfe0000 : $display("checkdata string %s ",pc_string);
'hfe0001 : $display("checkdata string %s ",pc_string);
'hfe0002 : $display("checkdata string %s ",pc_string);
'hfe0003 : $display("checkdata string %s ",pc_string);
[我必须先修复您代码中的语法错误,然后才能 运行 它。请确保您给我们 运行可用代码。]
问题是存储在 $line
中的内容。
您遍历 IN_FILE
文件句柄,直到找到包含 $pc_count$
的行。然后将该行复制到 $line
并进入内部处理循环。
所以第一次绕过你的内部循环,$line
包含:
'h$pc_count$ : $display("checkdata string %s ",pc_string);
几行之后,您使用以下代码更改 $line
:
$line =~ s/$pc_count$/$first/;
所以 $line
现在包含:
'hfe000 : $display("checkdata string %s ",pc_string);
下一次循环时,您从 $line
开始,其中包含以 hfe000
开头的字符串。因此,当您第二次尝试 运行 替换时,没有任何变化,因为 $line
不再包含 $pc_count$
.
最简单的修复可能是根本不更改 $line
,而是直接打印替换的输出(并使用 /r
,这样原始字符串就不会改变)。
if($_ =~ /([a-z0-9-]+)[\s]+/) {
my $first = ;
print $line =~ s/$pc_count$/$first/r;
}
产生:
'hfe0000 : $display("checkdata string %s ",pc_string);
'hfe0001 : $display("checkdata string %s ",pc_string);
'hfe0002 : $display("checkdata string %s ",pc_string);
'hfe0003 : $display("checkdata string %s ",pc_string);
我有两个输入文件:filea
和 fileb
。在filea
中,我有一个字符串$pc_count$
,在fileb
中,有数字和字符串。我必须在 fileb
中搜索数字并在 filea
中替换 $pc_count$
。
我试过下面的代码,但是 fileb
的第一个值每次都重复 ($pc_count$
)。
open (IN_FILE, "<filea") or die "Please provide an correct file path\n";
open (IN_FILE1, "<fileb") or die "Please provide an correct file path\n";
my $i = 0;
while (<IN_FILE>) {
if($_ =~ /$pc_count$/) {
my $line = $_;
while (<IN_FILE1>) {
if($_ =~ /([a-z0-9-]+)[\s]+/) {
my $first = ;
$line =~ s/$pc_count$/$first/;
#print OUT_FILE("$line");
print $line;
}
}
}
#print OUT_FILE("$_");
}
close IN_FILE;
close IN_FILE1;
filea
case(pc)
'h$pc_count$ : $display("checkdata string %s ",pc_string);
endcase
fileb
fe0000
fe0001
fe0002
fe0003
..
..
..
结果:
case(pc)
'hfe000 : $display("checkdata string %s ",pc_string);
'hfe000 : $display("checkdata string %s ",pc_string);
'hfe000 : $display("checkdata string %s ",pc_string);
'hfe000 : $display("checkdata string %s ",pc_string);
..
..
..
..
endcase
第一次替换后,$line
发生了变化,不再有字符串 $pc_count$
。解决此问题的一种方法是在另一个变量中保留原始行的副本 ($line2
):
use warnings;
use strict;
open (IN_FILE, "<filea") or die "Please provide an correct file path\n";
open (IN_FILE1, "<fileb") or die "Please provide an correct file path\n";
my $i = 0;
while (<IN_FILE>) {
if($_ =~ /$pc_count$/) {
my $line = $_;
while (<IN_FILE1>) {
my $line2 = $line;
if($_ =~ /([a-z0-9-]+)[\s]+/) {
my $first = ;
$line2 =~ s/$pc_count$/$first/;
print $line2;
}
}
}
#print OUT_FILE("$_");
}
close IN_FILE;
close IN_FILE1;
输出:
'hfe0000 : $display("checkdata string %s ",pc_string);
'hfe0001 : $display("checkdata string %s ",pc_string);
'hfe0002 : $display("checkdata string %s ",pc_string);
'hfe0003 : $display("checkdata string %s ",pc_string);
[我必须先修复您代码中的语法错误,然后才能 运行 它。请确保您给我们 运行可用代码。]
问题是存储在 $line
中的内容。
您遍历 IN_FILE
文件句柄,直到找到包含 $pc_count$
的行。然后将该行复制到 $line
并进入内部处理循环。
所以第一次绕过你的内部循环,$line
包含:
'h$pc_count$ : $display("checkdata string %s ",pc_string);
几行之后,您使用以下代码更改 $line
:
$line =~ s/$pc_count$/$first/;
所以 $line
现在包含:
'hfe000 : $display("checkdata string %s ",pc_string);
下一次循环时,您从 $line
开始,其中包含以 hfe000
开头的字符串。因此,当您第二次尝试 运行 替换时,没有任何变化,因为 $line
不再包含 $pc_count$
.
最简单的修复可能是根本不更改 $line
,而是直接打印替换的输出(并使用 /r
,这样原始字符串就不会改变)。
if($_ =~ /([a-z0-9-]+)[\s]+/) {
my $first = ;
print $line =~ s/$pc_count$/$first/r;
}
产生:
'hfe0000 : $display("checkdata string %s ",pc_string);
'hfe0001 : $display("checkdata string %s ",pc_string);
'hfe0002 : $display("checkdata string %s ",pc_string);
'hfe0003 : $display("checkdata string %s ",pc_string);