-1

Here I write Perl code but in if condition, used \n new line character not match.

#!/usr/bin/perl
use strict;

#use warnings;
use Cwd;
use File::Basename;
use File::Copy;

my $path = getcwd;

#print $path."\n";
opendir( INP, "$path\/" );
my @out = grep( /.(xml)$/, readdir(INP) );
close INP;

#print @out;
open( F6, ">Log.txt" );
foreach my $f1 (@out) {
    open( FF, "<$path\/$f1" ) or die "Cannot open file: $out[0]";
    my $data1 = join( "", <FF> );
    my @FILE_KA_ARRAY = split( /\n/, $data1 );
    my $file_ka_len = @FILE_KA_ARRAY;

    #print F6 $file_ka_len."\n";
    #print F6 $f."\t".$file_ka_len."\n";
    print F6 $f1 . "\n";
    for ( my $x = 1; $x < $file_ka_len; $x++ ) {
        my $y             = $x + 1;
        my $temp_file_arr = "";
        $temp_file_arr = $FILE_KA_ARRAY[$x];

        #print F6 $temp_file_arr."\t$x\n";
        my $temp1 = $temp_file_arr;
        if ( $temp1
            =~ m#(<list .*? depth="(\d+)">)\n?(<list .*? depth="(\d+)">)#gs )
        {
            my $list3 = $1;
            print F6 "\t\t\t\t\t\t\t\t" . $y . "\t\t" . $list3 . "\n";
        }
    }
}
Sobrique
  • 52,974
  • 7
  • 60
  • 101
Sagar
  • 43
  • 13

1 Answers1

4

Assuming your problem line is this:

if($temp1=~m#(<list .*? depth="(\d+)">)\n?(<list .*? depth="(\d+)">)#gs)

Then the problem here is here:

my @FILE_KA_ARRAY = split(/\n/, $data1);

Because your split is removing the linefeeds and putting each line into the array. And so when you do:

$temp_file_arr = $FILE_KA_ARRAY[$x];
my $temp1=$temp_file_arr;

You have no linefeeds in there, because you have no linefeeds in your source.

Additionally though:

  • Don't turn off warnings. IF you have warnings FIX THEM.
  • This looks like XML. Use a parser. (Although I'd avoid XML::Simple - it's nasty)
  • indenting your code is a good thing, because it helps clarify your code.
  • if you use glob ( "$path/*.xml" ) instead of readdir and grep you get a list of paths built in.
Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • Thanks Sobrique,Whats is my actual solution dear – Sagar Nov 05 '15 at 03:58
  • Given it is unclear what you are trying to accomplish, that is impossible to answer. With some sample input and desired output, that might change. – Sobrique Nov 05 '15 at 08:03
  • sorry Sobrique,i am try to get tags in xml file,but i m struct in regex thats include new line character(\n) – Sagar Nov 06 '15 at 04:07
  • Almost certainly easier that you think. Will need examples of input and output. – Sobrique Nov 06 '15 at 07:18
  • input is xml file and output will be 1 txt file contain line number of tag – Sagar Nov 06 '15 at 09:04
  • That's not enough. With an example in your question - or perhaps better yet, ask a new question. Post some (valid) XML that illustrates the your problem (need not be 'real' but needs to have same structure/attributes), post your code, post what you're aiming to get as output. This is almost certainly easier than you think with a parser. – Sobrique Nov 06 '15 at 09:12