Blockland Forums > Modification Help
Find The Closet Matching String
(1/5) > >>
jes00:
How would I find the closest matching string? For example if I did find("jes") it would find jes00.
aludane:
I've built a function yesterday for one of my scripts.

It should be useful.


--- Code: ---//THIS FUNCTION IS COMPARISON OF STRINGS
function ComparisonString(%StringInput,%StringSearch)
   {
      %ReturnTruth = strstr(%StringInput,%StringSearch);
      if(%ReturnTruth >= 0)
      {
         return true;
      }
   }
--- End code ---

It will return true if it finds a match. For example,

"Hello I am Jes00."

it will find "Jes" in the "Jes00" and then report it's location since strstr only outputs -1 when it cant find a match it would always be 0 or higher if it did find a match.

I only started learning Torque script at the weekend so I'm sure someone will come up with something more effecient.

Hope that helps.
DontCare4Free:

--- Quote from: aludane on March 19, 2012, 12:12:03 PM ---I've built a function yesterday for one of my scripts.

It should be useful.


--- Code: ---//THIS FUNCTION IS COMPARISON OF STRINGS
function ComparisonString(%StringInput,%StringSearch)
   {
      %ReturnTruth = strstr(%StringInput,%StringSearch);
      if(%ReturnTruth >= 0)
      {
         return true;
      }
   }
--- End code ---

It will return true if it finds a match. For example,

"Hello I am Jes00."

it will find "Jes" in the "Jes00" and then report it's location since strstr only outputs -1 when it cant find a match it would always be 0 or higher if it did find a match.

I only started learning Torque script at the weekend so I'm sure someone will come up with something more effecient.

Hope that helps.

--- End quote ---
He asked for the closest match, not the first match. Also, why not just do "strStr(%haystack, %needle) >= 0" instead of using a helper function?
aludane:

--- Quote from: DontCare4Free on March 19, 2012, 12:14:50 PM ---He asked for the closest match, not the first match. Also, why not just do "strStr(%haystack, %needle) >= 0" instead of using a helper function?

--- End quote ---
I was thinking of the string containing only one copy of what was needed to be found.

So if,

"jes" were to be compared against "jes00" and "jes0".

That would then require a comparison of "jes00" and "jes0". Perhaps some sort of lettercount against each other to see which one has the better matching number of letters.

"jes0" would be the closest match.

Is that correct or have I messed up?

My code must have gotten a bit messy yesterday. I've taken your advice and simplified it thanks.
DontCare4Free:

--- Quote from: aludane on March 19, 2012, 12:41:33 PM ---I was thinking of the string containing only one copy of what was needed to be found.

So if,

"jes" were to be compared against "jes00" and "jes0".

That would then require a comparison of "jes00" and "jes0". Perhaps some sort of lettercount against each other to see which one has the better matching number of letters.

"jes0" would be the closest match.

Is that correct or have I messed up?

--- End quote ---
Yeah. I guess you would first check if any started with the needle, and if so, filter the haystack to those. Then you could probably just get the shortest one.
Navigation
Message Index
Next page

Go to full version