Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [Lua] If-then always returning false  (Read 5108 times)

0 Members and 1 Guest are viewing this topic.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
[Lua] If-then always returning false
« on: December 14, 2013, 09:37:17 AM »
Maybe some fresh eyes can see what's wrong here.  In the function Write_Map() the 2nd If tree is always providing false, whereas the 3rd works the way it should.  It is suppose to grab the coordinates from pt.txt and then display any that are withing a -7 -> 7 (y) and -7 -> 7 (x) area around you (at pos.x , pos.y , pos.z).  It works for everything accept displaying a point from pt.

There should be rings of points.
Code: [Select]
args = {...}
pt_length = 0
pos = {}


function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

function readALL(file) -- by lhf on stackoverflow.com
local f = io.open(file, "rb")
local content = f:read("*all")
f:close()
return(content)
end

function split_space_String(s)
pt = {}
local k = 1
for v in string.gmatch(s, "([^\n]+)") do
if v ~= nil then
if v ~= "\n" then
pt[k] = { a = v }
pt_length = pt_length + 1
k = k + 1
end
end
end
end

function split_comma_String()
local b = 1
repeat
local k = 1
local tak = pt[b].a
if tak ~= nil then
for v in string.gmatch(tak, "[^,]+") do
_G["tmp" ..k] = v
k = k + 1
end
--                     x     y     z
pt[b] = { tmp1, tmp2, tmp3 }
print(b," ",pt[b][1],pt[b][2],pt[b][3])
end
b = b + 1
until b == pt_length
end


function Write_Map()
local w = 0
local y_l=7
local y = pos.y + 7
print("  -7-6-5-4-3-2-1 0+1+2+3+4+5+6+7")
repeat -- LOOP 1
local x = pos.x - 7
if string.len(y_l) == 1 then
if y_l ~= 0 then
y_l = "+" ..y_l
else
y_l = " " ..y_l
end
end
io.write(y_l .." ")


repeat -- LOOP 2
os.execute("title " ..x .."," ..y)
print(pt[2][1] .."," ..pt[2][2])
print(x .."," ..y)
local a = 2
local w = 0

repeat -- LOOP 3

if x == pt[a][1] then
if y == pt[a][2] then
io.write("X ")
w = 1
end
end

if x == pos.x then
if y == pos.y then
if w ~= 1 then
io.write("S ")
w = 1
end
end
end

if x == 0 then
if w ~= 1 then
io.write("| ")
w = 1
end
end

if y == 0 then
if w ~= 1 then
io.write("- ")
w = 1
end
end

a = a + 1
until a == pt_length-- Loop 3
os.execute("pause")
if w ~= 1 then io.write("0 ") end
w = 0
x = x + 1
until x == pos.x + 8 -- Loop 2
io.write("\n")
y = y - 1
y_l = y_l - 1
until y == pos.y - 8 -- Loop 1
end





split_comma_String(split_space_String(readALL("pt.txt")))

while true do
pos.x = round(tonumber(readALL("pos_x.txt")),0)
pos.y = round(tonumber(readALL("pos_y.txt")),0)
pos.z = round(tonumber(readALL("pos_z.txt")),0)

os.execute("title " ..pos.x .."," ..pos.y .."," ..pos.z)
Write_Map()
os.execute("timeout /t 2 /nobreak >nul")
os.execute("cls")
end

pt.txt (the first hundred lines or so)
Code: [Select]

330,300,-300,
328,310,-296,
328,310,-304,
328,304,-290,
328,304,-310,
328,296,-290,
328,296,-310,
328,290,-296,
328,290,-304,
324,318,-300,
324,300,-282,
324,300,-318,
324,282,-300,
322,320,-296,
322,320,-304,
322,304,-280,
322,304,-320,
322,296,-280,
322,296,-320,
322,280,-296,
322,280,-304,
320,322,-296,
320,322,-304,
320,320,-290,
320,320,-310,
320,310,-280,
320,310,-320,
320,304,-278,
320,304,-322,
320,296,-278,
320,296,-322,
320,290,-280,
320,290,-320,
320,280,-290,
320,280,-310,
320,278,-296,
320,278,-304,
318,324,-300,
318,300,-276,
318,300,-324,
318,276,-300,
310,328,-296,
310,328,-304,
310,320,-280,
310,320,-320,
310,304,-272,
310,304,-328,
310,296,-272,
310,296,-328,
310,280,-280,
310,280,-320,
310,272,-296,
310,272,-304,
304,328,-290,
304,328,-310,
304,322,-280,
304,322,-320,
304,320,-278,
304,320,-322,
304,310,-272,
304,310,-328,
304,290,-272,
304,290,-328,
304,280,-278,
304,280,-322,
304,278,-280,
304,278,-320,
304,272,-290,
304,272,-310,
300,330,-300,
300,324,-282,
300,324,-318,
300,318,-276,
300,318,-324,
300,300,-270,
300,282,-276,
300,282,-324,
300,276,-282,
300,276,-318,
296,328,-290,
296,328,-310,
296,322,-280,
296,322,-320,
296,320,-278,
296,320,-322,
296,310,-272,
296,310,-328,
296,290,-272,
296,290,-328,
296,280,-278,
296,280,-322,
296,278,-280,
296,278,-320,
296,272,-290,
296,272,-310,
290,328,-296,
290,328,-304,
290,320,-280,
290,320,-320,
290,304,-272,
290,304,-328,
290,296,-272,
290,296,-328,
290,280,-280,
290,280,-320,
290,272,-296,
290,272,-304,
282,324,-300,
282,300,-276,
282,300,-324,
282,276,-300,
280,322,-296,
280,322,-304,
280,320,-290,
280,320,-310,
280,310,-280,
280,310,-320,
280,304,-278,
280,304,-322,
280,296,-278,
280,296,-322,
280,290,-280,
280,290,-320,
280,280,-290,
280,280,-310,

pos_x.txt
Code: [Select]
325
pos_y.txt
Code: [Select]
300
pos_z.txt (rounded by round() to -300)
Code: [Select]
-300.25
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: [Lua] If-then always returning false
« Reply #1 on: December 14, 2013, 10:16:39 AM »
It turns out that the values in pt were saved as strings, so the statement was comaring strings to numbers and coming out false.  to fix this I used
Code: [Select]
if tonumber(x) == tonumber(pt[a][1]) then
if tonumber(y) == tonumber(pt[a][2]) then
if tonumber(pos.z) == tonumber(pt[a][3]) then
io.write("X ")
w = 1
end
end
end
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [Lua] If-then always returning false
« Reply #2 on: December 14, 2013, 11:45:30 AM »
Glad CH was able to help you.  ::)

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: [Lua] If-then always returning false
« Reply #3 on: December 14, 2013, 03:15:28 PM »
This tends to happen a lot.  I'll post a question, and then 5-10 min later, when I go away to do something else, I'll come up with an answer.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [Lua] If-then always returning false
« Reply #4 on: December 14, 2013, 05:51:42 PM »
Yes, no charge for out service.
Come back often.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [Lua] If-then always returning false
« Reply #5 on: December 14, 2013, 05:57:42 PM »
This tends to happen a lot.  I'll post a question, and then 5-10 min later, when I go away to do something else, I'll come up with an answer.
You successfully rubber-ducked the problem.
I was trying to dereference Null Pointers before it was cool.