How to get variables from term
Started by PixelFox, Apr 20 2015 03:21 PM
10 replies to this topic
#1
Posted 20 April 2015 - 03: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[]?
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[]?
#2
Posted 20 April 2015 - 03: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 by KingofGamesYami, 20 April 2015 - 03:31 PM.
#3
Posted 20 April 2015 - 03:32 PM
Lightning, on 20 April 2015 - 03:21 PM, said:
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 by Lupus590, 20 April 2015 - 03:32 PM.
#4
Posted 20 April 2015 - 03:42 PM
KingofGamesYami, on 20 April 2015 - 03:30 PM, said:
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
#5
Posted 20 April 2015 - 04: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"
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"
#6
Posted 20 April 2015 - 04: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 by Lupus590, 20 April 2015 - 04:42 PM.
#7
Posted 20 April 2015 - 04:39 PM
Thanks.
#8
Posted 20 April 2015 - 04:41 PM
i just patched a bug in my string concatenation code, you may want to check yours
Edited by Lupus590, 20 April 2015 - 04:43 PM.
#9
Posted 20 April 2015 - 04:46 PM
You could use table.concat instead.
#11
Posted 20 April 2015 - 05:10 PM
Now you do
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











