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

string.find() bug?

Started by bentallea, 15 December 2013 - 11:28 PM
bentallea #1
Posted 16 December 2013 - 12:28 AM

data = "test.rubikscuber1022.bentallea"
print(data:find(" ") --prints nil as expected
print(data"find(".") --prints 11
data= "akjdshgksnvb mclvvb sfjkgsfdv.saffg serg sggbsddfg w" --keyboard smashing just for the purpose of testing.
print(data:find(" ")) -- prints 1313
print(data:find(" ")) --prints 11
data = "test rubikscuber1022.bentallea"
print(data:find(" ")) --prints 55
print(data:find(".")) -- prints 11
this is running in the lua program under CCEmu with computercraft.jar version 1.5

the comments are the actual output. none of the print commands seemed to print the correct number other than then first one where the pattern was not in the string.
any help?
Grim Reaper #2
Posted 16 December 2013 - 12:40 AM
When you use string.find, it returns the start and end index of where in the string your pattern was found. So, 11 is really 1, 1 as the data[1] and data[1] are the start and end index of the first character in your string. This brings me to my next point: string.find uses patterns, so the . is actually an operator for 'any character' as far as lua is concerned. If you're looking for the period (.), then you need to do what is called "escaping" the pattern and trying '%.' as your pattern (escaping any pattern is done by using the % sign before the character you're looking for).

Here's an example which returns the number of times that a period appears in your string.

local data		= "test.rubikscuber1022.bentallea"
local occurrences = 0

-- Finds every occurrence of a period in the string 'data'.
for occurrence in data:match ("%.") do
	-- Since the only time the body of this loop is executed is when
	-- a period was found, we can safely increase the found number
	-- of occurrences of periods in our string.
	occurrences = occurrences + 1
end

This is a version using string.find:

local data		= "test.rubikscuber1022.bentallea"
local occurrences = 0
local startIndex = data:find ("%.")

local moreOccurrences = startIndex ~= nil -- Whether or not the there are more periods still to find.

while moreOccurrences do
	startIndex	  = data:find ("%.", startIndex + 1)
	moreOccurrences = startIndex ~= nil
end

Here is the lua.org tutorial on patterns.
Edited on 15 December 2013 - 11:59 PM
civilwargeeky #3
Posted 16 December 2013 - 12:44 AM
No this prints exactly as expected. (Except for the 4th one, it gave 1313 in my test).
string.find returns two values, a start and a stop. So your print was printing both the start and the stop, without a space.
In your second example, data:find(".") returned "11" because "." is a wildcard that represents any character. The first character matches this pattern, and there is only one character to match, so it has a start of 1 and an end of 1. If you were looking for a literal period, you should do "data:find("%.")"
For more help on string searching, go here: http://wiki.roblox.com/index.php/String_patterns