This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AndreWalia's profile picture

Make function arguments into a table?

Started by AndreWalia, 17 December 2012 - 09:24 AM
AndreWalia #1
Posted 17 December 2012 - 10:24 AM
im trying to make a function that makes the if statement w/ or shorter. for example

if One == "two" or One == "three" or One == "four" then
into

if is(One,"two","three","four") then
Is this even possible?
I don't have any code because I think if it would be possible before coding it. And I cant think of any ways to do that without making the arguments into a table.
By the way I'm talking of like

function is(fVariable,args)
not

function is(fVariable,equalOne,equalTwo,equalThree,equalFour)
etc.
theoriginalbit #2
Posted 17 December 2012 - 10:46 AM
you can declare a table inside of a function call like so

function doSomething( input )
  -- do something
end

doSomething( { One, "two", "three", "four" } )

hope this helps :)/>
Lyqyd #3
Posted 17 December 2012 - 12:11 PM
Yep, it's possible. Something like:


function is(base, ...)
    args = {...}
    if #args >= 1 then
        for i=1,#args do
            if base == args[i] then
                return true
            end
        end
    end
    return false
end