Game Development Community

comparing strings as greater and lesser to search an alphabetical list

by Warthog · in Torque Game Builder · 01/23/2009 (8:44 am) · 2 replies

Hi, I am trying to find a quicker way to sift through a large array of alphabetized words to find if the word input is in the list. I have been just comparing each word in the array to the input word, but as the list has gotten very long this is causing a delay. It occurred to me that I could move around in the list quicker if I could compare whether one letter is greater or lesser than the other. For instance, if my input word starts with a J, I could start in the middle of the array and compare J to the first letter of the word in the middle of the list, lets say it's an L and if I could determine that J is lesser than L, I could move my search up in the list by a certain amount and determine from there which way to look etc.

But < and > do not seem to work when comparing strings.

Any thoughts on how to do this, or perhaps a better approach to the problem?

Thanks!

#1
01/23/2009 (9:40 am)
A c-style strcmp function is available in Torquescript. That should be sufficient to implement binary search.

tdn.garagegames.com/wiki/TorqueScript_Console_Functions_3#strcmp.28_string1_.2C_...
#2
01/23/2009 (10:10 am)
Thanks, that should work!