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

string.find help

Started by ShadowDisruptor, 17 October 2014 - 06:47 PM
ShadowDisruptor #1
Posted 17 October 2014 - 08:47 PM
Hello! I am trying to use a sort of config string, but the code refuses to work. After about 30 minutes of debugging, I'm honestly stumped. The code should return true, but for some reason, it returns false.

[list]
[*]	[color=#000000]
[color=#AA9900][b]function[/b][/color] getValue[color=#66CC66]([/color]node[color=#66CC66])[/color][/color]
[*]	[color=#000000]
                [color=#AA9900][b]if[/b][/color]  [color=#AA9900][b]not[/b][/color] [color=#0000AA]string.find[/color][color=#66CC66]([/color]permString[color=#66CC66],[/color] [color=#66CC66]([/color][color=#FF6666]"+"[/color][color=#66CC66]..[/color]node[color=#66CC66]..[/color][color=#FF6666]"("[/color][color=#66CC66])[/color][color=#66CC66],[/color][color=#CC66CC]1[/color][color=#66CC66],[/color][color=#AA9900]true[/color][color=#66CC66])[/color] [color=#66CC66]==[/color] [color=#AA9900]nil[/color] [color=#AA9900][b]then[/b][/color][/color]
[*]	[color=#000000]
                        [color=#AA9900][b]return[/b][/color] [color=#AA9900]true[/color][/color]
[*]	[color=#000000]
                [color=#AA9900][b]else[/b][/color][/color]
[*]	[color=#000000]
                        [color=#AA9900][b]return[/b][/color] [color=#AA9900]false[/color][/color]
[*]	[color=#000000]
                [color=#AA9900][b]end[/b][/color][/color]
[*]	[color=#000000]
                [color=#0000AA]print[/color][color=#66CC66]([/color][color=#0000AA]string.find[/color][color=#66CC66]([/color]permString[color=#66CC66],[/color] [color=#66CC66]([/color][color=#FF6666]"+"[/color][color=#66CC66]..[/color]node[color=#66CC66]..[/color][color=#FF6666]"("[/color][color=#66CC66])[/color][color=#66CC66],[/color][color=#CC66CC]1[/color][color=#66CC66],[/color][color=#AA9900]true[/color][color=#66CC66])[/color][color=#66CC66])[/color][/color]
[*]	[color=#000000]
[color=#AA9900][b]end
	

Lignum #2
Posted 17 October 2014 - 09:02 PM
Oh dear, that's quite a conundrum you've got there. Let me decipher that:

function getValue(node)
  if not string.find(permString, "+" .. node .. "(", 1, true) == nil then
    return true
  else
    return false
  end
  print(string.find(permString, "+" .. node .. "(", 1, true))
end

Could you please tell us what exactly you're trying to do?
valithor #3
Posted 17 October 2014 - 09:10 PM
-snip

Sorry for the double post computer was acting up when i was posting this.
Edited on 17 October 2014 - 08:07 PM
valithor #4
Posted 17 October 2014 - 09:25 PM
It is hard to help here without a example of what you would input into the function, and what you expect to happen or the full code.

I will also point out that you still have unreachable code. The last time I tried to point this out you acted as if i did not understand the reason that you used return or that I did not understand how it worked. When you use return it stops doing anything in the function so no matter what it will never do that print.

function getValue(node)
  if not string.find(permString, "+" .. node .. "(", 1, true) == nil then
	return true -- function either ends here
  else
	return false -- or here
  end
  print(string.find(permString, "+" .. node .. "(", 1, true)) -- so it never reaches here
end

A example of a fix for this is

function getValue(node)
  if not string.find(permString, "+" .. node .. "(", 1, true) == nil then
	print(string.find(permString, "+" .. node .. "(", 1, true))
	return true
  else
	return false
  end
end

I do expect, however, that your problem lies where you define the permString variable. Without the full code it is not possible to help.

Edit:
Sorry for the double post my computer was freaking out while I was writing this.
Edited on 17 October 2014 - 08:07 PM
ShadowDisruptor #5
Posted 18 October 2014 - 05:57 PM
It is hard to help here without a example of what you would input into the function, and what you expect to happen or the full code.

I will also point out that you still have unreachable code. The last time I tried to point this out you acted as if i did not understand the reason that you used return or that I did not understand how it worked. When you use return it stops doing anything in the function so no matter what it will never do that print.

function getValue(node)
  if not string.find(permString, "+" .. node .. "(", 1, true) == nil then
	return true -- function either ends here
  else
	return false -- or here
  end
  print(string.find(permString, "+" .. node .. "(", 1, true)) -- so it never reaches here
end

A example of a fix for this is

function getValue(node)
  if not string.find(permString, "+" .. node .. "(", 1, true) == nil then
	print(string.find(permString, "+" .. node .. "(", 1, true))
	return true
  else
	return false
  end
end

I do expect, however, that your problem lies where you define the permString variable. Without the full code it is not possible to help.

Edit:
Sorry for the double post my computer was freaking out while I was writing this.

The returns are for debugging, I don't need it to print that. Since the code wasn't working, I added the returns to see where the issue is. An example of the string would be "help(Input here)" Sorry about the lack of information. An example of the function use would be:

help = "+help(Input data here)"
getValue(help)

EDIT:
Oops, was just coding Java. Changed code back to Lua.

If you want to see the full code, check it out here. http://pastebin.com/0GUjr64m
Edited on 18 October 2014 - 03:55 PM
valithor #6
Posted 18 October 2014 - 08:45 PM
-snip

In that example it is searching the permString variable, which is only defined in your reload function, for a string that is equal to "++help(Input data here)(". Yes i meant to put 2 +'s. And yes it is looking for a ( at the end. In the getValue function you put a + at the beginning of node and a ( at the end.

The way that the permString variable is defined it will only ever equal what the line before #configFile is equal to, or what the last line in the config file is if there is no line that equal #configFile. If, however, the first line in the config file is #configFile then permString will continue to equal nil. Seeing as the reloadConfig function is the only time it is defined with a actual value that is probably your problem.

I set up a little test program that I used for debugging that I will post here. I do not have time to help you actually solve it right now, but this appears to be the problem.

help = "+help(Input data here)"
permString = "++++++help(Input data here)(((((("
getValue(help)
print(string.find(permString, "+"..help.."(",1,true))
print("+"..help.."(")

I noticed that the "if not string.find() == nil" never returns true no matter what you put into it. Use the second thing in the code below.
Dont use this
if not string.find() == nil then
  do stuff
end
Use this
if string.find() then
  do stuff
end

problems
permString is being searched but most likely not defined with what you actually want.
searching permString for a string containing a extra + at the beginning, and a open parenthesis at the end
the if statement is not set up in a way where it will ever return true, check to see if the function is true using the code i provided and that part should work.
Edited on 18 October 2014 - 06:49 PM
ShadowDisruptor #7
Posted 19 October 2014 - 03:11 AM
-snip

Thanks. The reason I did the loop that way is because fs.readAll() wasn't returning anything so I had to come up with an alternative method. The issue is likely in the if statement always returning false. Also, why is it that it needs two +'s? Something weird with computercraft or something with my code? If it's with computercraft I'll likely just choose a different starting symbol. Thanks for the help.

EDIT:
After re-reading your debugging I realized you're misusing the function. The input is supposed to be a normal word like "chicken." The reason I use string.find("+"..node.."(") is so you don't need to enter +help(, it does it for you. Also, the goal is to find what's inside the ()'s, not to see if the string contains it. Once I figure out how to get this working I will use the returns from string.find to do so. Also, the reading should work. Since I'm using readLine it should switch to the next line and not read "#ConfigStart" until it's read the whole entire config file.
Edited on 19 October 2014 - 01:17 AM
ShadowDisruptor #8
Posted 19 October 2014 - 04:25 AM
Oh dear, that's quite a conundrum you've got there. Let me decipher that:

function getValue(node)
  if not string.find(permString, "+" .. node .. "(", 1, true) == nil then
	return true
  else
	return false
  end
  print(string.find(permString, "+" .. node .. "(", 1, true))
end

Could you please tell us what exactly you're trying to do?
Just realized what you meant :P/> I copied it from pastebin instead of my notepad, not sure why it added the bbcode. I got the first part working, now the goal is to get the parts on the inside of the ()'s. I'm getting this error: "bios:-1: attempt to concatenate nil and string" I did some debugging and found which line is causing this (Marked in code) and also printed all the values used which showed that none were nil.


--[[
Code name:
Footy's Nodes (A config API)

Coded by Cameron Lund
Bukkit: ShadowDisruptor
BukkitDev: ShadowDisrupter
Minecraft: footballfan12
Pastebin: footballfan12

License
Fair-use
--]]
permString = " "
tempString = " "
defaultConfig = {
"#ConfigFile",
"+banana(BananaPower)",
"+help(You shall not receive help!"
}

--Function to write the default config
function writeDefault()
for i=1, #defaultConfig do
configWrite.writeLine(defaultConfig[i])
end
configWrite.flush()
reloadConfig()
end

--Sets default config
function setDefault(tables)
defaultConfig = tables
end

--Function to load/reload the config string
function reloadConfig()
keepGoing = true
while keepGoing do
tempString = config.readLine()
if tempString == "#ConfigFile" then
while keepGoing do
tempString = config.readLine()
if tempString == nil then
keepGoing = false
else
if tempString == "#ConfigFile" then
keepGoing = false
else
permString = permString..tempString
end
end
end
keepGoing = false
end
end
end

--Function to set our config file
function setConfig(file)
if fs.exists(file) then
config = fs.open(file, "r")
reloadConfig()
configWrite = fs.open(file, "a")
end
end



--Gets the value of a node
function getValue(node)
if  string.find(permString, ("+"..node.."("),1,true) then
ignore, pos1 = string.find(permString, ("+"..node.."("),1,true)
pos2, ignore = string.find(permstring,(")"),pos1,true)--THIS LINE HERE. NOT SURE WHY THE ERROR....ABOVE LINE WORKS FINE
value = string.sub(permString,pos1,pos2)
return value
else
print("Node not found")
end
end

--Adds a singular value node
function addNode(node, data)
configWrite.writeLine("+"..node.."("..data..")")
end

--Turns table into list node
function addList(node, tables)
configWrite.writeLine("="..node.."{")
for i=1, #tables do
local tempTable = tables[i]
configWrite.writeLine(" -"..tempTable[1].."("..tempTable[2]..")")
end
configWrite.writeLine("="..node.."}")
end

--Closes the config files
function finish()
config.close()
configWrite.close()
end

EDIT: I'm not sure why it's all green… notepad++ shows no errors so must be the site. Also, sorry for being so needy. I've never had so many errors before. I can code decently for my age, which can be proved by my other decent-sized project: http://pastebin.com/JeA6epcb (Uses OpenPeripherals if you don't recognize some functions)
Edited on 19 October 2014 - 02:31 AM
valithor #9
Posted 19 October 2014 - 05:03 AM
-snip

Thanks. The reason I did the loop that way is because fs.readAll() wasn't returning anything so I had to come up with an alternative method. The issue is likely in the if statement always returning false. Also, why is it that it needs two +'s? Something weird with computercraft or something with my code? If it's with computercraft I'll likely just choose a different starting symbol. Thanks for the help.

EDIT:
After re-reading your debugging I realized you're misusing the function. The input is supposed to be a normal word like "chicken." The reason I use string.find("+"..node.."(") is so you don't need to enter +help(, it does it for you. Also, the goal is to find what's inside the ()'s, not to see if the string contains it. Once I figure out how to get this working I will use the returns from string.find to do so. Also, the reading should work. Since I'm using readLine it should switch to the next line and not read "#ConfigStart" until it's read the whole entire config file.

I used the function how your example showed to use it. :P/> Btw break can only be used to break loops, so the way you used it it breaks the first loop that it reaches above the if statement not the if statement itself.

Edit:
I see you updated the code, and fixed the break. The original code that i looked at had the error i mentioned.
Edited on 19 October 2014 - 03:12 AM