in reply to Searching through a file
Four problems. (Aside from not previewing your node. Use <c>...</c> around code in your nodes.)
You call your file handle TABLE, table and IN. The name is case-sensitive, and using the wrong name obviously won't work. Furthermore, you're better off using lexical file handles:
open(my $fh, '<', $inputs) or die ... while (<$fh>) { ... } close($fh); # Optional.
Similarly, you read each line into $line, yet you use $_ further on.
Third, keep in mind that the lines returned by the <HANDLE> operator include a trailing newline. Use chomp($line); as the first line of the while loop if you wish to remove this newline.
Finally, you treat $string as if it's a regexp while it's probably just text. (Added for clarification: If $string contains text as opposed to a regexp, you can convert the text to a regexp using \Q...\E or quotemeta. )
if ($line =~ /\Q$string\E/) { ... }
If you want to check for equality:
if ($line eq $string) { ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Searching through a file
by mr_mischief (Monsignor) on Sep 05, 2007 at 18:33 UTC |