Perl 两个文件之间的差异,并向更改的代码中添加代码

Perl 两个文件之间的差异,并向更改的代码中添加代码,perl,Perl,我有两个文件,A和B。B是从文件A派生出来的。我想创建一个文件C,所有代码都在B中,但如果文件A有变化,我需要在代码前后添加一些行。我已经尝试过逐行比较,将文件内容保存到数组中并遍历循环。但是,如果A中的行在B中,但不在同一行号,则此循环将不起作用。 我正在尝试的代码: my @file1 = <file1>; my @file2 = <file2>; #stored the file contents in an array. my @changes = ();

我有两个文件,A和B。B是从文件A派生出来的。我想创建一个文件C,所有代码都在B中,但如果文件A有变化,我需要在代码前后添加一些行。我已经尝试过逐行比较,将文件内容保存到数组中并遍历循环。但是,如果A中的行在B中,但不在同一行号,则此循环将不起作用。 我正在尝试的代码:

my @file1 = <file1>;
my @file2 = <file2>; #stored the file contents in an array.



my @changes = ();
my $line =0;

foreach (@file1)
{
  if ( defined( $file2[$line] ))
{
  
  if (($file2[$line] eq $file1[$line]) or ($file2[$line] eq $file1[$line+1]))
  { 
    push(@changes,$file1[$line]);
    $line = $line+1;
  }
  else
  {
      push(@changes,"//VCS coverage on\n");
      push(@changes,$file1[$line]); 
      $line = $line +1; 
  } 
}
else
{ 
   if ($file1[$line] =~ /endmodule/)
      {
        push(@changes,"//VCS coverage off\n");
      }
      push(@changes,$file1[$line]);
      $line = $line +1;
}
}


open(Changes_file,">","Changes_file.txt");
foreach (@changes)
{
  print Changes_file $_;
}
close Changes_file;
close file1;
close file2;
my@file1=;
my@file2=#将文件内容存储在数组中。
我的@changes=();
我的$line=0;
foreach(@file1)
{
如果(已定义($file2[$line]))
{
如果($file2[$line]eq$file1[$line])或($file2[$line]eq$file1[$line+1]))
{ 
推送(@changes,$file1[$line]);
$line=$line+1;
}
其他的
{
推送(@changes,//VCS覆盖范围在\n”);
推送(@changes,$file1[$line]);
$line=$line+1;
} 
}
其他的
{ 
如果($file1[$line]=~/endmodule/)
{
推送(@changes,//VCS覆盖范围关闭\n”);
}
推送(@changes,$file1[$line]);
$line=$line+1;
}
}
打开(Changes_file,“>”,“Changes_file.txt”);
foreach(@changes)
{
打印更改\u文件$\u;
}
关闭文件;
关闭文件1;
关闭文件2;
有谁能建议一个更好的方法吗?
//需要在代码btw A和B之间的差异结束的地方添加VCS覆盖范围关闭。

示例文件A和B以及所需的c将非常有用。请参阅每个文件中是否有可以用作文件之间匹配的密钥的内容?然后将文件A放入一个散列,遍历文件B,检查匹配的散列键,然后检查行是否有更改。@steveo314它只是文件中的SV代码,没有任何东西可以用作键。你能建议其他的方法吗。Thanks@JoeCasadonte非常感谢你的帮助。这将有助于:)