[Lua][Question] Multiple inputs in code? (IDK correct terminology)
#1
Posted 08 April 2013 - 02:19 PM
#2
Posted 08 April 2013 - 02:27 PM
local args = {...}
for i = 1,tonumber(tArgs[1]) do
print("star")
end
Or inside a script:
local times = read()
local valid = tonumber(times)
if type(valid) ~= "number" then
print("This is not a number")
return
else
for i = 1,valid do
print("star")
end
end
#3
Posted 08 April 2013 - 02:29 PM
variable = io.read() -- the first word being "star" is assumed.
if string.sub(variable, 6, 6) == nil then -- string.sub(variable, 6, 6) returns either the number or nil.
number = 1
else
number = tonumber(string.sub(variable, 6, 6))
end
for i = 1, number do
print("star")
end
(Sorry about no code container, I'm working off a mobile device.)
That should do what you want. Hope that helps!
Edit: Suicidal's is much cleaner!
#4
Posted 08 April 2013 - 02:56 PM
SuicidalSTDz, on 08 April 2013 - 02:27 PM, said:
local args = {...}
for i = 1,tonumber(tArgs[1]) do
print("star")
end
Or inside a script:
local times = read()
local valid = tonumber(times)
if type(valid) ~= "number" then
print("This is not a number")
return
else
for i = 1,valid do
print("star")
end
end#5
Posted 08 April 2013 - 03:23 PM
The command 'string.sub(string, starting character, ending character)' returns a sub-string or portion of the string you input, starting at the second argument and going to the third. For instance:
variable = "Test"
print(variable)
print(string.sub(variable, 1, 4))
This code will print "Test" twice. The first time is normal, then it prints a sub-string of variable starting at character 1 and going to character 4, which, in this case, is the entire string.
variable = "Test"
print(variable)
print(string.sub(variable, 1, 3))
This code will print "Test" and then "Tes." If you put the same number in the second and third arguments, it will give you the character at that point. print(string.sub(variable, 1, 1)) would print: "T".
'tonumber' is quite simple. It converts whatever is in its parentheses into a number if it can. The same works for 'tostring,' except it converts whatever is in its parentheses to a string. Very useful functions for when you are trying to convert input, a string, to a function that needs a number.
If a variable = "4", tonumber(variable) = 4.
In the code, variable = "star #". string.sub(variable, 6, 6) returns the 6th character of variable, which is the number. That is a string, and we need a number, so we use 'tonumber' to fix it.
I hope that clears things up!
#6
Posted 08 April 2013 - 11:27 PM
when you not give him a number there he will have a nil and he can not loop to a nil
to fix this just use this
number = tonumber(string.sub(variable, 6, 6)) or 1 -- or 1 sets number to 1 if the other condition is nil
other problem with the provided function is that it will error everytime there is no number at position 6 of the string.
if you type in "HelloKittySucks" 2
he will error or if you have fixed the error by adding or 1 he will just print it once not 2 times like you typed
Greets Loki
#7
Posted 08 April 2013 - 11:53 PM
number can be as big as you like. if you put in "Bimmelbahn 2132" he will print "Bimmelbahn" 2132 times
variable = read() space1 = string.find(variable, "%s") -- searches the string for space and returns the first occurence counter = string.sub(variable,space1+1,#variable) -- we cut the string starting at the point where he found the space and suggest that everything after the space is the number for the loop variable = string.sub(variable, 1, space1-1) -- now we cut the number of the inital string number = tonumber(counter) or false -- here we convert the entered counter to a number and set false if its not a number or not existing if so set value to false if not number then -- if number false Print error print "the second parameter must be a number" -- print error else for i = 1, number do print(variable) -- i print the text you entered before the number you can edit this if you want to print sth different end end
#8
Posted 09 April 2013 - 03:35 AM
#9
Posted 09 April 2013 - 03:53 AM
SuicidalSTDz, on 08 April 2013 - 02:27 PM, said:
local args = {...}
for i = 1,tonumber(tArgs[1]) do
print("star")
end
-snip-
I'd use SuicidalSTDz code. It's, by far, the simplest and easiest.
The only thing I would add would be to make it print once without any 2nd argument.
local tArgs = {...}
tArgs[1] = tArgs[1] or 1 -- if no 2nd argument is given, tArgs[1] is 1.
for i = 1,tonumber(tArgs[1]) do
print("star")
end
This way you can just type whatever word you want printed, and if you don't supply it with a number it automatically does it once.
#10
Posted 09 April 2013 - 03:55 AM
For your question about if substring etc..
Yes this would work.
To capture the error you can do the same thing I did for number to the space1 variable
Simply add or false
Then check if false and throw error message
#11
Posted 09 April 2013 - 04:30 AM
LordIkol, on 09 April 2013 - 03:55 AM, said:
For your question about if substring etc..
Yes this would work.
To capture the error you can do the same thing I did for number to the space1 variable
Simply add or false
Then check if false and throw error message
#12
Posted 09 April 2013 - 07:59 AM
#13
Posted 09 April 2013 - 05:41 PM
@suicidal
Somehow I agree with you when you want to have printed sth x number of times best thing is write a programm catching the arguments and print it.
Inside a running programm you could use a function with 2 times read first read for the text 2nd for the number of repeats.
But if you don't want to ask the user 2 times for entering text I don't see how you can do it without use of string libary.
Greets Loki
#14
Posted 09 April 2013 - 08:38 PM
variable = read() space1 = string.find(variable, "%s") -- searches the string for space and returns the first occurence if not space1 then counter = 1 else counter = string.sub(variable,space1+1,#variable) -- we cut the string starting at the point where he found the space and suggest that everything after the space is the number for the loop variable = string.sub(variable, 1, space1-1) -- now we cut the number of the inital string end number = tonumber(counter) or false -- here we convert the entered counter to a number and set false if its not a number or not existing if so set value to false term.clear() if not number then -- if number false Print error print "the second parameter must be a number" -- print error else if variable == "star" then for i = 1, number do print(variable) -- i print the text you entered before the number you can edit this if you want to print sth different end else print "wrong word" end end
#15
Posted 10 April 2013 - 03:17 PM
LordIkol, on 09 April 2013 - 08:38 PM, said:
variable = read() space1 = string.find(variable, "%s") -- searches the string for space and returns the first occurence if not space1 then counter = 1 else counter = string.sub(variable,space1+1,#variable) -- we cut the string starting at the point where he found the space and suggest that everything after the space is the number for the loop variable = string.sub(variable, 1, space1-1) -- now we cut the number of the inital string end number = tonumber(counter) or false -- here we convert the entered counter to a number and set false if its not a number or not existing if so set value to false term.clear() if not number then -- if number false Print error print "the second parameter must be a number" -- print error else if variable == "star" then for i = 1, number do print(variable) -- i print the text you entered before the number you can edit this if you want to print sth different end else print "wrong word" end end
else if blah == "blah" then for i =1, number do -- whatever was in the rest of this if and for. end else -- Code endYou cannot do that.... its should be
elseifnot
else ifAND PLEASE SPACE!!!! Oh yeah and open this spoiler for the spaced code
#16
Posted 10 April 2013 - 05:10 PM
#17
Posted 10 April 2013 - 06:20 PM
Spongy141, on 10 April 2013 - 03:17 PM, said:
LordIkol, on 09 April 2013 - 08:38 PM, said:
variable = read() space1 = string.find(variable, "%s") -- searches the string for space and returns the first occurence if not space1 then counter = 1 else counter = string.sub(variable,space1+1,#variable) -- we cut the string starting at the point where he found the space and suggest that everything after the space is the number for the loop variable = string.sub(variable, 1, space1-1) -- now we cut the number of the inital string end number = tonumber(counter) or false -- here we convert the entered counter to a number and set false if its not a number or not existing if so set value to false term.clear() if not number then -- if number false Print error print "the second parameter must be a number" -- print error else if variable == "star" then for i = 1, number do print(variable) -- i print the text you entered before the number you can edit this if you want to print sth different end else print "wrong word" end end
else if blah == "blah" then for i =1, number do -- whatever was in the rest of this if and for. end else -- Code endYou cannot do that.... its should be
elseifnot
else ifAND PLEASE SPACE!!!! Oh yeah and open this spoiler for the spaced code
The code i posted was working. Its not true that you can not do
else if
Maybe not nice but no error
Mistakes happen when you code on the road
And for the spacing i dont mind. Im not asking for help in this case and im typing a lot from the ipad so if you want spacing in my code put it in or dont read it
When i want help or post a really big code that is hard to follow i will add spacing.
But i dont care for a 20 lines code typed on the ipad.
Greets
#18
Posted 10 April 2013 - 06:46 PM
else ifIt didn't work and would give a error, so thats why you have to do (Or at least for me my programs never work unless I do a else if like this "elseif")
elseif
#19
Posted 10 April 2013 - 07:15 PM
1. Its Inefficient
2. Its looking bad
3. its more code cause the second if has to be ended as well
but it is defenetly working. When it was not working for you, you maybe treated the else if like an elseif without using a second "end". then you are right will not work. else it will work.
#20
Posted 10 April 2013 - 11:17 PM
local tArgs = { ... }
if #tArgs ~= 2 then
print( "Usage: text count" )
return
end
local text = tostring( tArgs[1] )
local count = tonumber( tArgs[2] )
for i = 1, count do
print(text)
end
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











