Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a string in a file 2

Status
Not open for further replies.

komark

Technical User
Sep 12, 2005
134
US
Hi,
I am trying to find a string that matches in a file.
Here is the code I have so far:

$find = "string";

open FILE, "<file.txt";

while(<FILE>)
{
if ($_ =~ /$find/)
{
$RES = "YES";
}
else
{
$RES = "NO";
}
}

close(FILE);

The string is present in the file and I am not getting the right answer. Please help

Thanks
 
Your script is not printing any "answer" to the screen or a file. Also, it is searching each line of the file, so if the search word is in the first line but not the second (for example), the scipt will assign a value of NO to $RES.

If you have to search the entire file do something like this:

Code:
$find = "string";
$RES = 'NO';
open FILE, "<file.txt";

   while(<FILE>)
   {
   if ($_ =~ /$find/)
     {
       $RES = 'YES';
     }
   }
  close(FILE);

print $RES;

Or if you just want to know the search word is present this would probably be a better way to go about it:

Code:
$find = "string";
$RES = 'NO';
open FILE, "<file.txt";

   while(<FILE>)
   {
   if ($_ =~ /$find/)
     {
       $RES = 'YES';
       [b]last; # terminates the 'while' loop early[/b] 
     }
   }
  close(FILE);

print $RES;






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Also, since $find is a regular expression, don't use double quotes for it: use 'qr' instead, i.e.

Code:
my $fine = qr/string/;
 
Thanks for the reply.

I did this:

$find = "string";

open FILE, "file.txt";

@line = <FILE>;
if ( grep (/$find/, @line))
{
$RES = "YES";
}
else
{
$RES = "NO";
}
# }

print "$RES\n";

close(FILE);
 
So it's still not working, then, as you haven't followed Kevin's suggestion. Unless you get lucky and it matches on the last line of the file, of course.
Perl:
use strict;
use warnings;

while (<DATA>) {
  if (/c/) {
    print "Found on line $.\n";
    last;
  }
}

__DATA__
a
b
c
d
e

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Steve,

it will work because he is now checking the return value of the grep() function instead of processing the file line by line.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes..i'm sorry it is working now. I should have been more specific. Thank you all for the time for helping me.

 
Sorry to Hijak the Thread but Ishnid's reply threw up a nagging phenomenom I keep finding crops up.

Why in perl is it so bad to qualify strings?

It's an instinct for anyone who has worked with Basic and other 4GL programming languages.

Wrapping quotes arround strings qualifies the variable as a string along with its contents especialy when your using a language that doesn't require pre-declared variables.

mytext = "SOME TEXT";

automatically defines it as a string rather than say having to write
Code:
Dim mytext AS String


Yet again and again I see the TT perl heads moan when ever they see it.

WHY?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
sorry i'm not sure I ws clear on my usage...
Code:
my sText = "Some Text";

&write_text("$sText");
or
Code:
$find = "string";
   if ($_ =~ /$find/)
     {
       $RES = 'YES';
     }

where Ishnid recommends
Code:
my $fine = qr/string/;

Whe in perl should you wrap strings in quotes and when shouldn't you , how to tell, and what's the difference

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
komark

My apologies, I should read more carefully before I shoot my mouth off in future...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Kevin,

Knew there was something to do with quoting strings now I understand why, especially if your variable has references or numbers, you don't want them to become stringyfied!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top