Hi, this should be a quick one. I've been using the tonumber() function and it works quite well. Until I go to use it for what I need it for. Converting a coordinate like this -62.5, -0.5, 84 to a number. It will return nil. Is there any other way to convert a string of numbers like that to a number?
Converting complex Strings to Numbers?
1 answer
This problem in general is called parsing.
When you have objects like Vector3s, the process of turning them into strings is called serializing or marshalling.
The process of undoing that (turning a string into an object) is called deserializing or unmarshalling.
You can use pattern matching to match a string against a given pattern.
The pattern here might be "some characters followed by a comma followed by some characters followed by a comma followed by some characters".
In Lua's pattern notation, we can write like
local x, y, z = someString:match("^(.-)%s*,%s*(.-)%s*,%s*(.-)$")
-- ^ means from-start-of-string
-- () means this is one of the values to return
-- . means any character
-- .- means zero-or-more characters
-- %s means a space character
-- %s* means zero-or-more space characters
-- $ means until-end-of-string
You'll get back nil if it couldn't match. You can then just try tonumber on each one.
local x, y, z = someString:match("^(.-)%s*,%s*(.-)%s*,%s*(.-)$")
if not x then
error("someString is not of the right form (match)")
end
x, y, z = tonumber(x), tonumber(y), tonumber(z)
if not x or not y or not z then
error("someString is not of the right form (tonumber)")
end
1 comment
Thanks! This will save me a lot of table space, much more efficient to parse and convert the string rather than save separate numbers to different table values
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
save Coordinates = {CoordinateX,CoordinateY,CoordinateZ} Works fine for me.
You want to convert 3 numbers into one number? What would the result look like?