Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Big problem in pattern matching

by kennethk (Monsignor)
on Jun 04, 2013 at 14:38 UTC ( #1036995=note: print w/ replies, xml ) Need Help??


in reply to Pattern Matching problem

There's a lot going on in your code; both as a debugging exercise and a convenience to those who would like to help you, you should try to simplify your posts down to the most compact code that replicates your issue. It has been my experience that bugs often identify themselves when I try to isolate them. See How do I post a question effectively?.

Your variable $string contains the escaped contents of the variable $test, which is never initialized in your code. I'm thinking this is an error in your post, not if your original code. Please make sure that posted code replicates what you are actually running, so we can debug the right thing. In this case, both strict and warnings would have flagged the issue. See Use strict warnings and diagnostics or die.

If I'm reading your spec right, your line featuring my $content =~ ... is supposed to grab the entire 'word' following your match. However, because you've used the binding operator (=~), you are actually running your regular expression against an uninitialized value (again, an issue caught by warnings). What you probably meant to do was

my ($content) = m/($string[a-z]*)/;

This runs the previous expression extended to include all trailing lower-case alphabetic characters, extracts the match, and uses list assignment to store the match (List value constructors). Easier would be to run the code as

if (/$string/) { m/($string[a-z]*)/; print "$1\n"; print "found string -$string- in line $num +\n"; print "<br>"; }
since capturing stores the value in $1 (Variables related to regular expressions). Note that I've removed the g modifier and escaped slashes because I don't think that's what you meant.

Let me know if I've missed the spec here.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: Big problem in pattern matching
Select or Download Code
Re^2: Big problem in pattern matching
by auhakim (Novice) on Jun 05, 2013 at 07:49 UTC

    I changed my code, and it works pretty find.

    #!/usr/bin/perl use CGI; my $cgi = new CGI; print $cgi->header('text/html'); if (defined $cgi->param('ok')) { my $dir = 'C:\\inetpub\\wwwroot\\vortragsreihe_DEV\\'; my $book = $dir.'dir.txt'; opendir(DIR, $dir) or die $!; open FILE, '>'.$book; while (my $file = readdir(DIR)) { if(-f $dir.$file){ # Use a regular expression to ignore files beginning with +a period next if ($file =~ m/^\./); print FILE "C:\\inetpub\\wwwroot\\vortragsreihe_DEV\\$file +\n"; } } close FILE; closedir(DIR); open (DATEI, '<' . 'C:\\inetpub\\wwwroot\\vortragsreihe_DEV\\dir.txt') + || die "Datei nicht gefunden $!"; my @daten = <DATEI>; close (DATEI); chomp(@daten); my $number = 0; my %params = $cgi->Vars; my $string = $cgi->param('string'); foreach(@daten){ print "finding -$string- in $daten[$number]\n"."</br>" +; open(my $file, "<", "$daten[$number]") or die "Can' +t open the file: $!"; my $num = 1; while (<$file>) { if (/$string/) { if (my ($match) = /(\Q$string\E\S*)/) { print "Matched '$string' in '$match' in li +ne $num"."<br />"; } # my $content =~ m/(\/$string\/[a-z]*)/g; # print $content; # print "found string -$string- in line $n +um \n"; # print "<br>"; } $num++; } $number++; } } else { print <<ENDHTML; <HTML> <BODY> <FORM name = "ariau" METHOD=GET ACTION="search.pl"> name: <INPUT TYPE=TEXT NAME="string" VALUE = ""><BR> <INPUT TYPE=SUBMIT NAME="ok" Value = "ok"> </FORM> </BODY> </HTML> ENDHTML }

    but the problem is, I can not search the string that started with particular symbol, such as, i cant search something like '$test', any help will be appreciated

      Remove
      if (/$string/) {

      or change it to

      if (/\Q$string\E/) {
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        and do you know how to make it case insensitive? I tried to change it with (/(\Q$string\E\S\i*)/) but it does not work -,-

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1036995]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2014-10-26 00:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls