This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gfcwfzkm's profile picture

using string.match with a table (variables)

Started by gfcwfzkm, 26 February 2014 - 08:32 PM
gfcwfzkm #1
Posted 26 February 2014 - 09:32 PM
Hi CC_Forum,

im trying to find a 'word' in a variable and i want to use table(s) cause there are many of these 'words'. But my Problem is, that it does nothing :o/>
Maybe its cause i still got some problems with for-loops and string.match() :(/>


  for ii,xx in pairs(badNPC) do
    if string.match(Str,ii) == true then
	  term.setTextColor(colors.blue)
	  return true
    end
  end
I want that it change the Textcolor if the variable 'Str' is in the table 'badNPC'. But well, it dont works good :/

(Here my Complete Code)
http://pastebin.com/paC8kRRx

What could be the solution for this?

Thanks for the Help

gfcwfzkm (Pascal)
Engineer #2
Posted 26 February 2014 - 09:50 PM
For this particular issue, I would just use the standard compare operator: ==
So, the function would look like this:

New function

local function checkMob( Str )
for ii,xx in pairs(badNPC) do
    if xx == Str then
      term.setTextColor(colors.blue)
      return true
    end
  end
  for ii,xx in pairs(goodNPC) do
    if  xx == Str then
      term.setTextColor(colors.lightBlue)
      return true
    end
  end
  for ii,xx in pairs(trusted) do
    if  xx == Str then
      term.setTextColor(colors.green)
      return true
    end
  end
end
gfcwfzkm #3
Posted 26 February 2014 - 10:10 PM
I cant use == cause the variable contains behind some numbers (from the NPCs). Now i changed string.match(Str,ii) to string.match(Str,xx) but it still dont wanna work :/

CometWolf #4
Posted 26 February 2014 - 10:21 PM
String.match dosen't return true, it returns the matched string, so this would always be false

	if string.match(Str,ii) == true then
Just drop the "== true", this way it will perform the if block aslong as the result of string.match is not nil or false.

	if string.match(Str,ii) then
Edited on 26 February 2014 - 09:22 PM