Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Big problem in pattern matching

by kennethk (Abbot)
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.

Replies are listed 'Best First'.
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=""> 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

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1036995]
[Discipulus]: new $workstation is arrived: SSD 250, sata 1TB, 16G RAM, only i5 CPU.. seems a good one: guess what first user installation will be?
[Corion]: Discipulus: Notepad++, or Perl ;)
[marto]: xubuntu or lubuntu
[Corion]: :-D
[marto]: my desktop is a core2 duo, 120GB SSD, 4GB RAM, boots to (GUI) login in less than 10 seconds from cold start
[Discipulus]: mah, winbuntu 10...
[marto]: whent he boys start school I'll build myself as new machine, ryzen based
[Corion]: marto: That's cool! I think my (home) desktop spends 30 seconds in the BIOS...
[Corion]: marto: Heh - currently they seem promising, but I think I'll stay with nVidia+Intel for the time being, as I've been bitten too often by bad AMD drivers
Discipulus is (still?) not a big fan of notepad++

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-07-27 08:52 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (407 votes). Check out past polls.