Back to OmaTips

Searching for a string match, and Search Engines

Zigma sent me a basic search engine example, and that is what got me going for doing up this tip.

Searching for a string is easy when the @StrStr() proc is used...
  Syntax: @StrStr(TextToSearchIn,TextToSearchFor)

@StrStr() is a way to compare two strings of text for a match. Think of the proc name as "if String = String".
@StrStr() can be a little confusing because of how it returns an error. @StrStr() returns a zero based index of where the first character in the string to search for was found. Otherwise it returns a value of "-1" if the string wasn't found. So the easiest way to check for a string is to just look if it does not equal "-1", that means it's a match!

That means: Say we're looking for the string "Cuddles" and you see:
  A dog named Cuddles piddles on your leg.
and you check it with:
  /action {%0 piddles on your leg.} {/if {@StrStr($0,Cuddles)!=-1)} {slap Cuddles}}

So, if "$0" contains "Cuddles" then @StrStr()'s value will be 0 or greater (0 based index). If the string "Cuddles" isn't in "$0" then @StrStr() will return -1.
So just remember, if it's not equal to -1, it's in the string.

Search Engines

So, with @StrStr() it's possible to make some "search engines" for whatever you want. Commonly scripts are made to search a list or array for some string:
/loop {1,@GetCount(SomeList)} {/if {@StrStr(@GetItem(SomeList,$LoopCount),TextToSearchFor)!=-1} {Then this item contains the string that you searched for} {Else this item Doesn't contain the string that you searched for}}

Tips

Use spaces to help narrow down the search. Say you make a search engine to look up strings in a huge list of clansays. Supplying a space can help so you don't get text that matches in the middle of a word.
For example, you want to search for "url" in a huge list of clansays. /if {@StrStr($0, url )!=-1} ..would only find the specific word " url ", so you wouldn't get a hit on a word like "Durle said, ..."

Like all of MM, it's case sensitive. abc!=ABC. So use @Lower() or @Upper() where appropriate.
/if {@StrStr(@Lower($0),url)!=-1}


Back to OmaTips