Posted 03 February 2015 - 09:51 AM
So I have a string:
I need to ge the 12 and the 91 into two separate variables as a number.
Changing to a number is simple, the problem I have is that number may not always be two digits, like it could have: sector1x1 or sector134x225.
So I need to split the string from sector to get the last set of numbers, but then somehow iterate over it grabbing the first numbers then using the 'x' add the next few numbers into another variable.
So I need to get "sector12x21" to:
Any help? I have the following code from others but this is annoying me because I can get the numbers, but only as one number, or in a table… :(/>
Anyway sorry here is the code I have:
(It's a mess but the code after the multi-line comment to the last two lines is what I have):
Thanks guys! :
(EDIT: it is indented on my code, it just never indents on this forum :(/>)
EDIT: Answered, the code was:
str = "sector12x91"
I need to ge the 12 and the 91 into two separate variables as a number.
Changing to a number is simple, the problem I have is that number may not always be two digits, like it could have: sector1x1 or sector134x225.
So I need to split the string from sector to get the last set of numbers, but then somehow iterate over it grabbing the first numbers then using the 'x' add the next few numbers into another variable.
So I need to get "sector12x21" to:
worldxid = 12
worldyid = 21
Any help? I have the following code from others but this is annoying me because I can get the numbers, but only as one number, or in a table… :(/>
Anyway sorry here is the code I have:
(It's a mess but the code after the multi-line comment to the last two lines is what I have):
--[[local worldname = "space12x7"
local worldsector = string.sub(worldname, 6)
local worldxid = string.sub(worldsector, 1, 1)
local worldyid = string.sub(worldsector, 3)
print("X: "..worldxid..", Y: "..worldyid)]]
local tNumbers = {
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
}
str = "space1x1"
something = {}
new = {}
for i = 1, #str do
local c = str:sub(i,i)
-- do something with c
print(c)
table.insert(something, c)
end
for k, v in ipairs(something) do
for _,v1 in ipairs(tNumbers) do
if v == v1 then
table.insert(new, v)
elseif v == "x" then
break
end
end
end
table.concat(new)
print(#new)
print(textutils.serialize(new))
s = "space1x1"
res={}
local data = str:gsub('%d%d%d',function(n)res[#res+1]=tonumber(n)end)
Thanks guys! :
(EDIT: it is indented on my code, it just never indents on this forum :(/>)
EDIT: Answered, the code was:
str = "something123x456"
local s1, s2 = str:match("(%d+)x(%d+)")
local n1, n2 = tonumber(s1), tonumber(s2)
Edited on 03 February 2015 - 11:20 AM