I was making a better tonumber function. now when i try to find a . in a string it always returns 1
[CODE]
print( string.find( "this is text...", ".") )
output = 1
[/CODE]
I need help
Lua uses patterns in string.find, and dot is a function if you will, not a character. In the second argument of string.find, precede the dot with a percentage sign.
Also, wrong forum.
[QUOTE=James xX;51224129]Lua uses patterns in string.find, and dot is a function if you will, not a character. In the second argument of string.find, precede the dot with a percentage sign.
Also, wrong forum.[/QUOTE]
I think by "Reg Lua" he meant non-GLua.
Set the fourth argument of string.find to true to disable patterns.
The question isn't explicitly GLua, so it's not the wrong forum
OP, read some of this (in particular the first 2 paragraphs):
[url]https://www.lua.org/pil/20.1.html[/url]
[quote]The most powerful functions in the string library are string.find (string Find), string.gsub (Global Substitution), and string.gfind (Global Find). They all are based on patterns.
Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching. The main reason for this is size: A typical implementation of POSIX regexp takes more than 4,000 lines of code. This is bigger than all Lua standard libraries together. In comparison, the implementation of pattern matching in Lua has less than 500 lines. Of course, the pattern matching in Lua cannot do all that a full POSIX implementation does. Nevertheless, pattern matching in Lua is a powerful tool and includes some features that are difficult to match with standard POSIX implementations.[/quote]
Patterns are ways of finding not just specific strings of characters, but [I]types[/I] of characters at all. [I]patterns[/I] of characters.
Here's some more info on patterns:
[url]https://www.lua.org/pil/20.2.html[/url]
Notice in the table listed, it says "." means "all characters". This means that searching for the `.` pattern will return the first character which matches that pattern, which is the first character, which means it will return 1 (the first index).
As James said, you need to "escape" the pattern by using %, like `%.`
This tells Lua that the character after the % is not to be treated as it normally would be. It goes both ways, though: searching for the pattern "a" will return the first occurrence of "a". Searching for %a tells Lua that you're not looking for the letter `a`, but the pattern %a, which means "any character that's a letter".
Let's say you wanted to look for the letters "%a" (literally a percent, and then an a) as in the string: "%a + %c = %d". You can escape an escape by doing %%a.
The most common type of patterns are regular expressions. It's not relevant to your case, since Lua doesn't use regular expressions, but if you wanted to know more about patterns in general (and if you ever felt like learning any other programming languages, like C), you could look up regular expression. Another common type of pattern syntax is Perl syntax (Perl is a language designed to search text, mostly).
Sorry, you need to Log In to post a reply to this thread.