How to compare string from table from function with string?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

How to compare string from table from function with string?

Post by Bugala »

I could propably figure this one out myself too, but since I have participated in game making contest and time is starting to run out (less than 4 days for deadline) i better continue programming from elsewhere and let you guys tell me how to do this.

Problem is (example code):


Function comparestrings(string)
if table1.text = string then something
Endfunction

Table1 = { text = "currenttext" }
Table2 = { text = "new text" }

comparestring(table2.text)



I thought this would work so, that it compares "currenttext" with "new text". However, i dont know how Hollywood works in this case, but it gives me error about "Cant compare string to number".

When i used

DebugPrint(table1.text, string)

right before the line "If table1.text=string)" it shows that table1.text contains "currenttext" and string contains "new text".

Yet despite that, it complains it cant compare string with number?

What am i doing wrong here, what am i not understanding?
jalih
Posts: 276
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: How to compare string from table from function with string?

Post by jalih »

I think you have error in some other place in your code.

Something like this should just work:

Code: Select all

Function StrCmp(str1, str2)
   If str1 = str2
      DebugPrint("Supplied strings are the same")
   Else
      DebugPrint("Supplied strings are different")
   EndIf
EndFunction



Table1 = {}
Table1.text = "Hello"

Table2 = {}
Table2.text = "Hello"

StrCmp(Table1.text, Table2.text)

WaitLeftMouse()
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: How to compare string from table from function with string?

Post by Bugala »

Thanks, i believe that StrCmp will work. I will try it today.
Post Reply