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

Using Input After A Program Name (E.g. "divide 100")

Started by NomNuggetNom, 20 August 2013 - 05:07 PM
NomNuggetNom #1
Posted 20 August 2013 - 07:07 PM
I've written a program to tell you how many stacks + extra you'll need of a certain number of items. It's a very simple code that will ask users "How many items?" then wait for an input and use that number. I was hoping to skip the step of asking a user, and just allow an input like "divide 100" to directly come up with an answer. Here's an exampe:


Here's my code (pastebin):
Spoiler

print("How many items do you need?")
numberInput = read()

stackNumber = math.floor(numberInput/64)
remainder = numberInput - (stackNumber*64)

solution = "You'll need " ..tostring(stackNumber).. " stack(s) and " ..tostring(remainder).. " extra."
term.write(solution)
H4X0RZ #2
Posted 20 August 2013 - 07:24 PM
Look at this code

local tArgs = {...}
for k,v in pairs(tArgs) do
  print(v)
end
if you run this program like this "test hello world"
(test represents the program name)
it would output that:

hello
world
so, the … represents the arguments after the program name.
If you want to only output one argument do it like this:

local tArgs = {...} --you can call it what you wan't
print(tArgs[1]) --replace the 1 with the position of the argument you want to output/handle etc.
you can have more than one variables for it too!

local arg1,arg2,arg3 = ...
--Argument handling should be here, but I'm to lazy to code...

I hope I helped you
NomNuggetNom #3
Posted 21 August 2013 - 11:10 AM
Thanks man, that's perfect! I changed the code to this: http://pastebin.com/mpmrq2EY