718 posts
Location
Hawaii
Posted 01 June 2012 - 02:03 AM
Go here for the real post.Real post:So the problem is not with the table, but the strings inside it. Add a print(v) inside the for loop, so you can see what the strings looks like:
for k, v in pairs(Players) do
print(v)
...
end
If they don't contain the "/" your looking for it will give you empty strings.
It printed the x/y correctly, so i tried printing it like this:
for i,kik in pairs(Players) do
v = tostring(kik)
if v then
minedX = string.sub(v, 1, string.find(v, "/")-1)
minedY = string.sub(v, string.find(v, "/")+1, string.len(v))
print("MinedX - MinedY")
print(minedX.. " - " ..minedY)
if tonumber(minedX) then
term.setCursorPos(tonumber(minedX),tonumber(minedY))
write(i)
end
end
end
it printed this:
x/y -
So i'm having trouble with the string.sub, minedX is the entire string and minedY is nil.
Help? :/
1604 posts
Posted 01 June 2012 - 03:13 AM
It should work, did you get an error?
Using the pairs function in the for loop iterates through every key-value pair on the table, and the values can't be nil, so you don't have to check that.
Example:
local t = {}
t[1] = "a"
t[100] = "b"
t["hello"] = "c"
for k, v in pairs(t) do
print(k, ": ", v)
end
It would print:
1: a
100: b
hello: c
(possibly not in that order)
718 posts
Location
Hawaii
Posted 01 June 2012 - 04:42 AM
It should work, did you get an error?
Using the pairs function in the for loop iterates through every key-value pair on the table, and the values can't be nil, so you don't have to check that.
Example:
local t = {}
t[1] = "a"
t[100] = "b"
t["hello"] = "c"
for k, v in pairs(t) do
print(k, ": ", v)
end
It would print:
1: a
100: b
hello: c
(possibly not in that order)
MinedX and MinedY return nil
52 posts
Posted 01 June 2012 - 04:35 PM
Can you tell us the exact error?
1604 posts
Posted 01 June 2012 - 05:59 PM
So the problem is not with the table, but the strings inside it. Add a print(v) inside the for loop, so you can see what the strings looks like:
for k, v in pairs(Players) do
print(v)
...
end
If they don't contain the "/" your looking for it will give you empty strings.
718 posts
Location
Hawaii
Posted 02 June 2012 - 08:52 AM
So the problem is not with the table, but the strings inside it. Add a print(v) inside the for loop, so you can see what the strings looks like:
for k, v in pairs(Players) do
print(v)
...
end
If they don't contain the "/" your looking for it will give you empty strings.
It printed the x/y correctly, so i tried printing it like this:
for i,kik in pairs(Players) do
v = tostring(kik)
if v then
minedX = string.sub(v, 1, string.find(v, "/")-1)
minedY = string.sub(v, string.find(v, "/")+1, string.len(v))
print("MinedX - MinedY")
print(minedX.. " - " ..minedY)
if tonumber(minedX) then
term.setCursorPos(tonumber(minedX),tonumber(minedY))
write(i)
end
end
end
it printed this:
x/y -
So i'm having trouble with the string.sub, minedX is the entire string and minedY is nil.
Help? :/
8543 posts
Posted 02 June 2012 - 09:41 AM
What data are you separating? Why not just do:
minedX, minedY = string.match(v, "(%-?%d+)/(%-?%d+)")
This will grab positive and negative integers. For pure alphanumerics, you'd use (%w+) for each match instead of (%-?%d+).
1604 posts
Posted 02 June 2012 - 08:14 PM
There's a bug in LuaJ that makes that function not work sometimes. There's a workaround for that, but I think it's better to do it like Lyqyd said and use string.match, it's shorter and it searchs for numbers only. If you want to use string.sub anyway, try concatenating an empty string to the result of string.sub, like this:
local s = string.sub("Some string", n)..""
This is the post with the bug and that "fix".