in reply to Re^2: how to search for a string in a particular file?
in thread how to search for a string in a particular file?

tm86:

When testing your code, you should give it some non-trivial test cases. For example, if you use this:

Iow is the time for all good men to come to the aid of their party. I scream you scream, we all scream for ice cream. Or was it icecream?

looks like it would give you the result:

did not find the string did not find the string found string icecream

Is that what you intended?

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^4: how to search for a string in a particular file?
by tm86 (Initiate) on May 29, 2012 at 13:35 UTC
    #!/tools/packarch/public/perl/5.8.8/bin/perl use strict; use warnings; open(my $file, "<", "input.txt") or die "Can't open input.txt: $!"; my $string = "icecream"; while (<$file>) { if (/$string/) { print "found string $string\n"; } else { my $notfound = "not found"; } } if (my $notfound eq "not found") { print "string not found\n"; }

    I'm seeing this error now: Use of uninitialized value in string eq at ./testScript.pl line 19, <$file> line 8.

    What does this mean? the file input.txt does not contain the string "icecream" so ideally i should see the output at "string not found"

    ps. thank you guys for all your help!

      tm86:

      Each time you use "my" you declare a new variable. So at the line in question, you're declaring a new variable called $notfound, and then testing it to see if it contains "not found" before you ever set it to a value.

      Here's a quickie to show you what I mean:

      #!/usr/bin/perl use warnings; my $s='the quick red fox'; print $s, "\n"; my $s='Foo'; print $s, "\n"; { my $s="Bar"; print $s, "\n"; my $s; print $s, "\n"; } print $s, "\n";

      When I run it, I get:

      $ perl t.pl "my" variable $s masks earlier declaration in same scope at t.pl line +5. "my" variable $s masks earlier declaration in same scope at t.pl line +10. the quick red fox Foo Bar Use of uninitialized value $s in print at t.pl line 11. Foo

      There are several things to notice here:

      • If you use warnings, perl will tell you when you declare a new variable that has the same name as one you already have (the first two lines of output from my script).
      • When you start a new block, you can create a new version of a variable without perl giving you an error message (only two warnings for the four declarations).
      • When a block goes out of scope, then the previously-hidden (masked) variable is available again. (The "Foo" at the end.)
      • Immediately after creating a variable, it is undefined (the "use of uninitialized value" warning).

      So you're close, but you need a couple tweaks:

      #!/tools/packarch/public/perl/5.8.8/bin/perl use strict; use warnings; open(my $file, "<", "input.txt") or die "Can't open input.txt: $!"; my $string = "icecream"; my $notfound = ""; while (<$file>) { if (/$string/) { print "found string $string\n"; } else { $notfound = "not found"; } } if ($notfound eq "not found") { print "string not found\n"; }

      As you can see, I put a declaration at the top and initialized it to "". Then removed all the extra declarations.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        thanks! that worked like a charm. i understand the issue now... i seemed to be declaring it multiple times and no initialization. That will help my main code a lot!