if l == tbl.y - 1 and line:sub( tbl.x, tbl.x ) == "#" then --#I want to compare the character to #, U, or D. But I really dont want to write out 12 more statements.
--turtle should move forward
return function() orient( "up" ) return turtle.forward() end, {x = tbl.x, y = tbl.y - 1, z = tbl.z}
elseif l == tbl.y and line:sub( tbl.x - 1, tbl.x - 1 ) == "#" then
--turtle should move "left"
return function() orient( "left" ) return turtle.forward() end, {x = tbl.x - 1, y = tbl.y, z = tbl.z}
elseif l == tbl.y and line:sub( tbl.x + 1, tbl.x + 1 ) == "#" then
--turtle should move "right"
return function() orient( "right" ) return turtle.forward() end, {x = tbl.x + 1, y = tbl.y, z = tbl.z}
elseif l == tbl.y + 1 and line:sub( tbl.x, tbl.x ) == "#" then
--turtle should move back
return function() orient( "down" ) return turtle.forward() end, {x = tbl.x, y = tbl.y + 1, z = tbl.z}
elseif oldChar:upper() == "U" then
--turtle should move up
return function() return turtle.up() end, {x = tbl.x, y = tbl.y, z = tbl.z + 1}
elseif oldChar:upper() == "D" then
--turtle should move down
return function() return turtle.down() end, {x = tbl.x, y = tbl.y, z = tbl.z - 1}
end
Multiple and/or statements
Started by KingofGamesYami, Jul 01 2014 02:22 PM
5 replies to this topic
#1
Posted 01 July 2014 - 02:22 PM
So, I'm making a map function, and in part of it it has a large amount of comparing, in which I need several or statements... What is the best way of comparing this? Do I need to write out a bunch more elsif statements, or is there an easier way? I don't think I can use multiple or statements, but maybe there is another way.
#2
Posted 01 July 2014 - 02:34 PM
Are you going for something along these lines?:
Or maybe something like this?:
local compareTo = {"#","U","D"}
for i=1,#compareTo do
if l == tbl.y - 1 and line:sub( tbl.x, tbl.x ) == compareTo[i] then
.
.
.
end
end
Or maybe something like this?:
local compareTo = {"#","U","D"}
-- Returns the index of a given value if it exists in a given table, or nil if it does not.
local function whereIn(targetTable, targetValue) for i=1,#targetTable do if targetTable[i] == targetValue then return i end end end
if l == tbl.y - 1 and whereIn(compareTo, line:sub( tbl.x, tbl.x )) then
.
.
.
#3
Posted 01 July 2014 - 07:59 PM
I could write a function to compare it yes,
Edit: To give you an idea of how I'm using it, heres the rest of the code
local function compare( value ) if value == "#" then return true elseif value:upper() == "D" then return true elseif value:upper() == "U" then return true else return false end end if l == tbl.y - 1 and compare( line:sub( tbl.x, tbl.x ) ) then
Edit: To give you an idea of how I'm using it, heres the rest of the code
Spoiler
Edited by KingofGamesYami, 01 July 2014 - 08:04 PM.
#4
Posted 02 July 2014 - 01:34 AM
why not make a lookup table with anonymous functions?
#5
Posted 02 July 2014 - 01:43 PM
I actually just thought of something that (might) be slightly better
local compare = {
["#"] = true, --#like this?
D = true,
U = true,
d = true,
u = true,
}
if <statement> and compare[ value ] then
</statement>
Edited by KingofGamesYami, 02 July 2014 - 02:00 PM.
#6
Posted 02 July 2014 - 01:51 PM
except you'd need to put that # into a string, otherwise Lua will complain.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











