Computing.Net > Forums > Programming > Perl: Searching a String

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Perl: Searching a String

Reply to Message Icon

Name: Michael McKenzie
Date: September 19, 2002 at 13:17:47 Pacific
OS: XP
CPU/Ram: Big and Lots
Comment:

I think Tom once helped me out on this syntax, but I forgot where I put it now... :(

I can successfully search a File Handle (MYDATA) for a search value ($SEARCH) in a while loop like the following:

while (){
if (/$SEARCH/){ ... do something}
}

BUT HOW DO I search a STRING for a $SEARCH Value? I know it is something similar, but can't remember.

How do I search to see if "cow" ($SEARCH) is in the following string ($VALUE)?

$VALUE = "The cow jumped over the moon";
if ($VALUE = /$SEARCH/) { ... do something... }

I know this is not the right syntax, but can someone point me in the right direction ? I am not worried about case sensitive issues, and I am not looking for an index function to tell me how many times it appears in the string, I just want a quick and easy TRUE/FALSE type situation if the "cow" can be found in the $VALUE string.

Thanks so much for your help in advance~!

- Michael




Sponsored Link
Ads by Google

Response Number 1
Name: Anna Kournikova
Date: September 19, 2002 at 17:44:26 Pacific
Reply:

($string =~ /pattern/) ## true if the pattern is found somewhere in the string


so I think you want to use something like:

if ("the cow jumped over the moon" =~ /cow/)


0

Response Number 2
Name: Michael McKenzie
Date: September 20, 2002 at 09:10:40 Pacific
Reply:

THANK YOU SO MUCH~! :)

- Michael


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Perl: Searching a String

Searching a String inside a Class www.computing.net/answers/programming/searching-a-string-inside-a-class/16020.html

searching a string in a text file www.computing.net/answers/programming/searching-a-string-in-a-text-file/17972.html

search inside a process 4 a string www.computing.net/answers/programming/search-inside-a-process-4-a-string/13206.html