Arrays Perl:将文件内容与数组内容相匹配

Arrays Perl:将文件内容与数组内容相匹配,arrays,regex,perl,file-io,Arrays,Regex,Perl,File Io,我有一个数组@arr1,其中每个元素的形式为#定义一个B 我还有另一个文件,f1,内容如下: #define,x,y #define,p,q 等等。我需要检查每行的第二个值(y,q等)是否与数组中任何元素的第一个值匹配。示例:假设数组有一个元素#define abc 123,文件有一行#define,hij,abc 当出现这种匹配时,我需要将行#define hij 123添加到数组中 while(<$fhDef>) #Reading the file

我有一个数组
@arr1
,其中每个元素的形式为
#定义一个B

我还有另一个文件,
f1
,内容如下:

#define,x,y
#define,p,q
等等。我需要检查每行的第二个值(
y
q
等)是否与数组中任何元素的第一个值匹配。示例:假设数组有一个元素
#define abc 123
,文件有一行
#define,hij,abc

当出现这种匹配时,我需要将行
#define hij 123
添加到数组中

while(<$fhDef>)               #Reading the file
{
    chomp;
    $_ =~ tr/\r//d;
    if(/#define,(\w+),(\w+)/)
    {
        my $newLabel = $1;
        my $oldLabel = $2;
        push @oldLabels, $oldLabel;
        push @newLabels, $newLabel;
    }
}

      foreach my $x(@tempX)             #Reading the array
      {
            chomp $x;
            if($x =~ /#define\h{1}\w+\h*0x(\w+)\h*/)
            {
                my $addr = $1;
                unless(grep { $x =~ /$_/ } @oldLabels) 
                {
                    next;
                }
                my $index = grep { $oldLabels[$_] eq $_ } 0..$#oldLabels;
                my $new1 = $newLabels[$index];
                my $headerLabel1 = $headerLabel."X_".$new1;
                chomp $headerLabel1;
                my $headerLine = "#define ".$headerLabel1."0x".$addr;
                push @tempX, $headerLine;
            }
         }
while()#读取文件
{
咀嚼;
$\ux=~tr/\r//d;
如果(/#定义,(\w+),(\w+/)
{
my$newLabel=$1;
my$oldLabel=$2;
按@oldLabels,$oldLabel;
推送@newLabels,$newLabel;
}
}
foreach my$x(@tempX)#读取数组
{
咀嚼$x;
如果($x=~/#define\h{1}\w+\h*0x(\w+\h*/)
{
my$addr=$1;
除非(grep{$x=~/$\u/}@oldLabels)
{
下一个
}
my$index=grep{$oldLabels[$\]eq$\}0..$\\ oldLabels;
my$new1=$newLabels[$index];
my$headerLabel1=$headerLabel.“X”.$new1;
chomp$headerLabel1;
my$headerLine=“#定义“$headerLabel1.”0x“$addr;
按@tempX,$headerLine;
}
}

这只是挂起。毫无疑问,在我面前我遗漏了一些东西,但是什么???

标准的方法是使用散列。使用第一个参数作为键对数组进行散列。然后遍历文件并检查散列中是否存在密钥。我使用了一个HoA(数组散列)来处理每个键的多个值(参见最后两行)

#/usr/bin/perl
使用警告;
严格使用;
my@arr1=(“#定义y x”,
“#定义abc 123”,
);
我的%hash;
对于(@arr1){
my($arg1,$arg2)=(拆分“”)[1,2];
推送{$hash{$arg1}},$arg2;
}
而(){
咀嚼;
我的($arg1,$arg2)=(拆分/,/)[1,2];
if($hash{$arg2}){
为@{$hash{$arg2}打印“#定义$arg1$\n”;
}
}
__资料__
#定义,x,y
#定义,p,q
#定义,hij,abc
#定义,荷航,abc

规范的方法是使用散列。使用第一个参数作为键对数组进行散列。然后遍历文件并检查散列中是否存在密钥。我使用了一个HoA(数组散列)来处理每个键的多个值(参见最后两行)

#/usr/bin/perl
使用警告;
严格使用;
my@arr1=(“#定义y x”,
“#定义abc 123”,
);
我的%hash;
对于(@arr1){
my($arg1,$arg2)=(拆分“”)[1,2];
推送{$hash{$arg1}},$arg2;
}
而(){
咀嚼;
我的($arg1,$arg2)=(拆分/,/)[1,2];
if($hash{$arg2}){
为@{$hash{$arg2}打印“#定义$arg1$\n”;
}
}
__资料__
#定义,x,y
#定义,p,q
#定义,hij,abc
#定义,荷航,abc

正如另一个答案所说,最好使用散列。另外,请记住,你正在做一件事

foreach my $x(@tempX)
但你也在做一件事

push @tempX, $headerLine;

这意味着您正在修改要迭代的数组。这不仅仅是一种不好的做法,这也意味着你很可能会因此产生一个无限循环。

正如另一个答案所说,最好使用散列。另外,请记住,你正在做一件事

foreach my $x(@tempX)
但你也在做一件事

push @tempX, $headerLine;

这意味着您正在修改要迭代的数组。这不仅是一种不好的做法,也意味着您很可能会因此产生一个无限循环。

感谢您提供了一个优雅的解决方案。感谢您提供了一个优雅的解决方案。