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

How to get variables from term

Started by PixelFox, 20 April 2015 - 02:21 PM
PixelFox #1
Posted 20 April 2015 - 04:21 PM
Hello again. I'm trying to make a program that takes an input from the terminal. For example:

Hello would be the name of the program
If I type in:
"Hello 10"
It would print "Hello World" 10 times.

If I type in:
"Hello 1100"
It would print "Hello World" 1100 times.


Is it like a = args[]?
KingofGamesYami #2
Posted 20 April 2015 - 04:30 PM

local tArgs = { ... }

…will give you a table that looks something like this:


local tArgs = {
  "10",
}

In addition to usage in programs, it can be used in function declared like this:


local function t( ... )
--#stuff
end
local t = function( ... )
--#stuff
end
Edited on 20 April 2015 - 02:31 PM
Lupus590 #3
Posted 20 April 2015 - 04:32 PM
Is it like a = args[]?

close.

args = {…}
or
anyVariableName = {…}

although args is the convention

you can access them like so:

if you typed in "hello 10"
args[1] would equal 10

if you typed in "program this is a test"
args[1] would be "this"
args[2] would be "is"
args[3] would be "a"
args[4] would be "test"
Edited on 20 April 2015 - 02:32 PM
PixelFox #4
Posted 20 April 2015 - 04:42 PM

local tArgs = { ... }

…will give you a table that looks something like this:


local tArgs = {
  "10",
}

In addition to usage in programs, it can be used in function declared like this:


local function t( ... )
--#stuff
end
local t = function( ... )
--#stuff
end
Thanks!! :D/>
PixelFox #5
Posted 20 April 2015 - 05:27 PM
New problem:
How to I turn L3 = {"H", "i", "t", "h", "e", "r", "e"} into a string?
Example:
L4 = "lol"
L3 = {"H", "i", "t", "h", "e", "r", "e"}
– CODE HERE to make L3 become L4–
print(L4)
– it would print "Hithere"
Lupus590 #6
Posted 20 April 2015 - 05:36 PM
concatenate the strings

chars = {"t","e","s","t"}
string = "" --this is required else you may have a mess at the front of your string
for i = 1, #chars do --I should have started at 1 not 0
  string = string..chars[i]
end
Edited on 20 April 2015 - 03:42 PM
PixelFox #7
Posted 20 April 2015 - 05:39 PM
Thanks.
Lupus590 #8
Posted 20 April 2015 - 05:41 PM
i just patched a bug in my string concatenation code, you may want to check yours
Edited on 20 April 2015 - 03:43 PM
Lyqyd #9
Posted 20 April 2015 - 05:46 PM
You could use table.concat instead.
Lupus590 #10
Posted 20 April 2015 - 05:55 PM
You could use table.concat instead.

didn't know that that existed
Creator #11
Posted 20 April 2015 - 06:10 PM
Now you do ;)/>