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

[solved]checking entered string with a table

Started by CreeperGoBoom, 29 May 2014 - 05:02 AM
CreeperGoBoom #1
Posted 29 May 2014 - 07:02 AM
ok here is what i want to do

ok so ive set up a computer, put in a pastebin get, run the program for the first time config
computer asks what side the modem is on, i am trying to run a check using this function:

local function sidevalid(str)
  local input = str
  local sidecount=6
  for i2 = 1,6 do
	if input ~= sides[i2] then
	  local sidecount = sidecount-1
	  if sidecount == 0 then
		local output=false
	  else
		local output=true
	  end
	end
  end
return output
end

this is where its used:

local function config ()--this bit works fine
  mkfile(pcinfo)
  print("insert name for this client pc")
  id = read()
  setstr(pcinfo,id)
  shell.run("label set "..id)
  print("input modem side, must be valid")
  mside = read()
  if not sidevalid(mside) then
	repeat
	  sideerror()
	  mside = read()
	until sidevalid(mside)
	setstr(pcinfo,mside)
  elseif sidevalid(mside) then
	setstr(pcinfo,mside)
  end
  print("what side do you want as the redstone output?")
  rside=read()
  if not sidevalid(rside) then
	repeat
	  sideerror()
	  rside = read()
	until sidevalid(rside)
	setstr(pcinfo,rside)
	elseif sidevalid(rside) then
	setstr(pcinfo,rside)
  end
  print("What server channel will this client be using?")
  channel = tonumber(read())
  channelfeed = tostring(channel)
  print("channel entered:"..channelfeed)
  print("querying matching server, please be patient")
  modem=peripheral.wrap(mside)
  local state = false
  if not state then
	modem.open(channel)
	local timeout = os.startTimer(3)
	modem.transmit(channel,channel,channelfeed)
	local event = {os.pullEvent()}
	print("transmitting info")
	if event[1] == "modem_message" then --this section of code just wont fire
	  print("server query success!")
	  setstr(pcinfo,channel)
	  mkfile("config")
	  print("config complete, rebooting in...")
	  state = true
	  for i = 3,0,-1 do
		if i == 0 and state then
		   shell.run("reboot")
		end
	  print(tostring(i).."...")
	  sleep(1)
	end
  elseif event[1] == "timer" then
	state=false
	print("no response or wrong channel number entered, please reboot and try again...")
	print("aborting config and rebooting!")
	sleep(1)
	shell.run("reboot")
  end
end
end

when that code is triggered after the side is entered, my sideerror function fires, which means sidevalid is returning false, which means i am using a wrong method of a check

any suggestions get a thumbs up :D/>

heres the link to full code :D/>

http://pastebin.com/qufnLXtb
Edited on 29 May 2014 - 05:35 AM
CCJJSax #2
Posted 29 May 2014 - 07:18 AM
Why not make it search for any attached peripheral that is a modem instead of making the user say so?
CreeperGoBoom #3
Posted 29 May 2014 - 07:20 AM
good idea, thanks :D/>

however i still need the string check for the next step down

"what side do you want redstone to output, must be valid"
Bomb Bloke #4
Posted 29 May 2014 - 07:23 AM
Rather, sidevalid is returning nil. You're declaring "output" as local to the "if" blocks in which you define it, and so its content is discarded when those blocks end - before you attempt to return.

Redeclaring "sidecount" as a new local variable after already declaring it earlier in the function will cause other problems.

Here's a version of the function which would operate along the lines you're thinking (assuming you've correctly filled the "sides" table):

local function sidevalid(str)
	local sidecount = 6

	for i2 = 1, 6 do
		if str:lower() ~= sides[i2] then sidecount = sidecount - 1 end  -- Note "local" isn't re-used here.
	end

	return sidecount > 0
end

It could be made more efficient, though:

local function sidevalid(str)
	for i2 = 1, 6 do
		if str:lower() == sides[i2] then return true end
	end

	return false
end

For the purposes of detecting a modem (or any other given peripheral), you'd be better off taking advantage of the commands available in the peripheral API.
Edited on 29 May 2014 - 05:23 AM
CreeperGoBoom #5
Posted 29 May 2014 - 07:32 AM
perfect, thanks Bomb Bloke, Kudos to you my friend :thumbsup:

[solved]
Edited on 29 May 2014 - 05:35 AM