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

string.find bug

Started by Pharap, 25 November 2012 - 05:35 AM
Pharap #1
Posted 25 November 2012 - 06:35 AM
For some reason, whenever I use string.find on a string without a "." to find a ".", it returns 11 instead of nil.

I'm running MC version 1.4.2 and CC version 1.46.
example code:

print(string.find("hello world","."))--prints 11, should print nil
Cloudy #2
Posted 25 November 2012 - 06:45 AM
string.find is broken in this implementation of Lua. You are best replacing it with your own function - or appending a blank string to the variable you are using.
PixelToast #3
Posted 25 November 2012 - 07:25 AM
common mistake

its because "." represents all characters
read this:
http://www.lua.org/manual/5.1/manual.html#5.4.1

in order to do that you will need to escape it:
print(string.find("hello world","%."))
Cloudy #4
Posted 25 November 2012 - 11:19 AM
Ah. My point still stands though :D/>/>
Cruor #5
Posted 25 November 2012 - 01:30 PM
Yeah, to use string.find() you simply do… string.find(str.."", pattern) :D/>/> it be borked
immibis #6
Posted 01 December 2012 - 08:36 PM
CC could fix it in bios.lua, by including a wrapper that just appends "" - but it would need to be fixed in the metatable too so str:find("stuff") works.
PixelToast #7
Posted 02 December 2012 - 09:01 AM
CC could fix it in bios.lua, by including a wrapper that just appends "" - but it would need to be fixed in the metatable too so str:find("stuff") works.
metatables of strings are blocked in the bios.lua so you wouldnt need to use anything other than lua
Pharap #8
Posted 02 December 2012 - 09:41 AM
I should have got this closed ages ago, I found out that someone thought it was a good idea to use . as a pattern shortly after starting this post.

common mistake

its because "." represents all characters
read this:
http://www.lua.org/m...nual.html#5.4.1

in order to do that you will need to escape it:
print(string.find("hello world","%."))

I really hate that page, the layout is awkward.
Cloudy #9
Posted 02 December 2012 - 10:22 AM
Your wish is my command.