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

[Lua] [Error] Identifying program argument types

Started by GodlyPlexi, 07 February 2013 - 03:49 PM
GodlyPlexi #1
Posted 07 February 2013 - 04:49 PM
Title: [Lua] [Error] Identifying program argument types

I was making a program to test out if I could identify if an arugment of a program, like [umbrella 1 this], could be determined and then the program would follow up with the appropriate action like, to print "Hey, yo man, that arguemnt is text!" Or something like that. Every time I try to identify the argument's type with type() it returns a nil value, instread of string or number.

I tried to set up two programs to do similar things, but for simplicity, I'll post my most recent one I've done.


tArgs = { ... }
table1 = {1, "stringman", 1.5}
print(type(#tArg[1]))
sleep(1)
print(table1[1])
sleep(1)
print(table1[2])
sleep(1)
print(table1[3])
sleep(1)
print(type(table1[1]))
sleep(.5)
print(type(table1[2]))
sleep(.5)
print(type(table1[3]))
sleep(.5)
x = "string"
y = type(x)
print(y)

It gives me the error
testman:4: attempt to index ? (a nil value)
Can type() not determine arugments' type for some reason? I looked around the forums and didn't see anything on this, so I'm turning to you guys. Please help?
GopherAtl #2
Posted 07 February 2013 - 06:54 PM
you set tArgs first, then try to acces tArg[1] instead of tArgs[1]
tesla1889 #3
Posted 07 February 2013 - 06:55 PM
if one of the args is nil, then it might error

EDIT: nvm, didnt catch the variable name error
remiX #4
Posted 07 February 2013 - 07:03 PM
Loop through all arguments:


Args = {...}

for index, value in pairs(Args) do
    Tmp = tonumber(value) or ((value == "true" or value == "false) and value == "true") or value -- attempts to set the value to a number, ifnot a boolean, ifnot itself
    print("[" .. index .. "] Is type [" .. type(tostring(Tmp)) .. "]")
end
ChunLing #5
Posted 08 February 2013 - 10:07 AM
Aside from the error in matching the identifier assigned the argument table, there is also the fact that all of the arguments are passed in as strings. You can deal with that by looping through to check to see if they can be converted to a number or boolean, and just leaving them the original value if they can't be (which is what the above code by remiX does). You should be aware that the argument table has an extra value to track how many other values were passed in from the command line…it's a number rather than a string so you can make a way to strip it off if you want.