in reply to Re^3: Pattern Matching problem
in thread Pattern Matching problem

I hope you dont considered those code as ton of lines codes. and the input file is just the list of directory which should be scanned

Replies are listed 'Best First'.
Re^5: Pattern Matching problem
by hdb (Monsignor) on Jun 04, 2013 at 13:16 UTC

    I have tried to clean it up a bit but no guarantee as I cannot run it...

    • I have changed the loop over the files to eliminate $number.
    • I also eliminated $num as $. already contains the line number.
    • I changed the line with my $content ... to catch the match rather than comparing $content with the pattern.
    • $test is not defined anywhere in your code fragement.
    • You test for match with $string in the if clause, but then you populate $content based on a different match: /$string/[a-z]*. If the former matches the latter might still fail! This might cause your problem.

    # what is in $test ? Not specified. my $string = quotemeta("$test"); foreach my $filename (@daten){ print "finding -$string- in $filename\n"."</br>"; open(my $file, "<", $filename) or die "Can't open the file: $!" +; while (<$file>) { if (/$string/) { print; # NEW STATEMENT my ($content) = m/(\/$string\/[a-z]*)/g; # different match +, might fail print $content; print "found string -$string- in line $. \n"; # $. is the +line number print "<br>"; } } }

    UPDATE: added a print statement to check hypothesis

      the $test here recognized as a string, so the program will find the string that contain $test

Re^5: Pattern Matching problem
by marto (Cardinal) on Jun 04, 2013 at 12:59 UTC

      yup sorry, im in my deadline. I'll make sure that my next post match the quality standard of the post

        I think you misunderstand me, I commenting on your "can i just post my whole code?" question. Sure you can, however it's easier for people to point out problems with a short script that shows your problem. Often there are extra parts we don't need to know about. You can include the data as well. I wasn't being critical of your post.

Re^5: Pattern Matching problem
by hdb (Monsignor) on Jun 04, 2013 at 14:32 UTC

    If $string is "a", then \/$string\/[a-z]* does NOT match "air". It would match "/a/ir".

    $string = "a"; print "success 1\n" if "air" =~ /$string/; print "success 2\n" if "air" =~ /\/$string\/[a-z]/; print "success 3\n" if "/a/ir" =~ /\/$string\/[a-z]/; print "success 4\n" if "air" =~ /$string[a-z]/; # UPDATE