0

I am working with an XML file and I am trying to check whether a line contains a specified String in the entire document. Reading the file seems to work and splitting the String into an array was also successful. But my code doesn't print the lines that contain the specified string and I can't figure out what's wrong. I've tried three different ways to check whether the String is contained (see below). Thanks for helping!

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];
    }
}

The file is an XML file.

1 Answers1

0

You have converted content ot the file into lower case version.
$daten1 = lc $daten1;

You look for string with uppercase letter.
if(index($datenarray1[$i], '795.25-M') != -1)

Look for lowercase version if the string (795.25-m) or use regular expression with i modifier.
if($datenarray1[$i] =~ /795\.25-M/i )

BTW 1) your code is very "unperlish" IMHO. 2) You may look for the string in the first while loop (during processing file line by line).

AnFi
  • 10,493
  • 3
  • 23
  • 47
  • Thanks, I am a total Perl beginner but I'll try to improve my code. I've deleted my lowercase command but print still won't work even though I am 100% sure that the specified String is in the document and the reading process was successful. Any suggestions? – LittleEntertainer Jun 18 '17 at 19:12
  • Include a few lines from the processed file. – AnFi Jun 18 '17 at 20:24