【问题标题】:Replace $variable which changes every iteration in while loop替换 $variable 在while循环中改变每次迭代
【发布时间】:2021-12-08 23:36:32
【问题描述】:

我有两个输入文件:fileafileb。在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 = $1; 
                                $line =~ s/\$pc_count\$/$first/;
                                #print OUT_FILE("$line");
                                print $line;

                        }

                }

        }
        #print OUT_FILE("$_");
}
close IN_FILE;
close IN_FILE1;

文件

case(pc)
    'h$pc_count$ : $display("checkdata string %s ",pc_string);
endcase

文件b

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

【问题讨论】:

    标签: regex perl


    【解决方案1】:

    [我必须先修复代码中的语法错误,然后才能运行它。请确保您给我们可运行的代码。]

    问题在于$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 = $1;
        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);
    

    【讨论】:

      【解决方案2】:

      在您第一次替换后,$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 = $1; 
                                      $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);
      

      【讨论】:

        猜你喜欢
        • 2021-11-03
        • 1970-01-01
        • 2021-05-30
        • 1970-01-01
        • 2018-03-15
        • 2020-01-05
        • 1970-01-01
        • 2014-12-12
        • 2015-03-23
        相关资源
        最近更新 更多