我正在处理一个XML文件,并试图检查一行是否在整个文档中包含指定的字符串。读取文件似乎工作,并将字符串拆分成数组也是成功的。但我的代码不打印包含指定字符串的行,我无法弄清楚什么是错的。我试过三种不同的方法来检查字符串是否被包含(见下文)。 感谢您的帮助!检查数组是否包含指定的字符串在Perl中
use strict;
use locale;
my($daten1, $daten2, @datenarray1, @datenarray2, $datenarray1, $datenarray2, $i, $j, $l);
open (FHIN, '0a_nur_Text_nur_795.xml') || die "Die Datei 0a_nur_Text_nur_795.xml konnte nicht geoeffnet werden: $!";
while(<FHIN>) {
$daten1=$daten1.$_; #Daten als String einlesen
}
close (FHIN);
$daten1 = lc $daten1;
@datenarray1= split(/\n/,$daten1);
for($i=0; $i<@datenarray1; $i++)
{
#if(grep { $_ eq '795.25-M'} $datenarray1[$i])
#if($datenarray1[$i] =~m/795.25-M/)
if(index($datenarray1[$i], '795.25-M') != -1)
{
print $datenarray1[$i];
}
}
该文件是一个XML文件。
该文件是什么样子?请[编辑]你的问题,并加入[mcve]。 –