Jump to content




How to get variables from term


  • You cannot reply to this topic
10 replies to this topic

#1 PixelFox

  • Members
  • 106 posts

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[]?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 20 April 2015 - 03:32 PM

View PostLightning, 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 PixelFox

  • Members
  • 106 posts

Posted 20 April 2015 - 03:42 PM

View PostKingofGamesYami, 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
Thanks!! :D

#5 PixelFox

  • Members
  • 106 posts

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"

#6 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

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 PixelFox

  • Members
  • 106 posts

Posted 20 April 2015 - 04:39 PM

Thanks.

#8 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

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 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 April 2015 - 04:46 PM

You could use table.concat instead.

#10 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 20 April 2015 - 04:55 PM

View PostLyqyd, on 20 April 2015 - 04:46 PM, said:

You could use table.concat instead.

didn't know that that existed

#11 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 20 April 2015 - 05:10 PM

Now you do ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users