I want to make a little command line program, but it fails when I try to run a program with args!
How do I detect args in a string like this:
al arg1 arg2
al arg1 arg2
local tArgs = { ... }
for k, v in pairs( tArgs ) do
print( v )
end
I get userinput and want to seperate it, so I can run a program with the args the user has typed.
Because
shell.run(read()) not works and I dont want to have multiple reads for one program.
local function split(strI,regEX)
str = string.gsub(strI, regEX, " ")
str = str .. " "
local last = 1
local strTable = {}
for i=1, str:len() do
if str:sub(i,i) == " " then
local subStr = str:sub(last, i - 1)
table.insert(strTable, subStr)
last = i + 1
end
end
return strTable
end
shell.run(unpack(split(read()," ")))
There is nothing…I get userinput and want to seperate it, so I can run a program with the args the user has typed.
Because
shell.run(read()) not works and I dont want to have multiple reads for one program.
Try this:
uhm… Using this code it and we run `program hi Im Engineer`:I get userinput and want to seperate it, so I can run a program with the args the user has typed.
Because
shell.run(read()) not works and I dont want to have multiple reads for one program.
local tArgs = { ... }
for k,v in pairs( tArgs ) do
print(k)
end
--> hi
--> Im
--> Engineer
tArgs[1] --> hi
tArgs[2] --> Im
tArgs[3] --> Engineer
local function runProg()
local prog = read()
local tProg = {}
for word in prog:gmatch("[^%s]+") do
tProg[#tProg + 1] = word
end
shell.run( unpack( tProg ))
end
shell.run( read():lower() )
It should work properlyThere is nothing…I get userinput and want to seperate it, so I can run a program with the args the user has typed.
Because
shell.run(read()) not works and I dont want to have multiple reads for one program.
Try this:
If anyone don't understand what I mean.
Look at CraftOS. You can enter programs with args, how?
local function split(strI,regEX) str = string.gsub(strI, regEX, " ") str = str .. " " local last = 1 local strTable = {} for i=1, str:len() do if str:sub(i,i) == " " then local subStr = str:sub(last, i - 1) table.insert(strTable, subStr) last = i + 1 end end return strTable end shell.run(unpack(split(read()," ")))
local function runLine( _sLine )
local tWords = {}
for match in string.gmatch( _sLine, "[^ \t]+" ) do
table.insert( tWords, match )
end
local sCommand = tWords[1]
if sCommand then
return run( sCommand, unpack( tWords, 2 ) )
end
return false
end
This is how i'd do it, much more efficient split functionTry this:local function split(strI,regEX) str = string.gsub(strI, regEX, " ") str = str .. " " local last = 1 local strTable = {} for i=1, str:len() do if str:sub(i,i) == " " then local subStr = str:sub(last, i - 1) table.insert(strTable, subStr) last = i + 1 end end return strTable end shell.run(unpack(split(read()," ")))
local function split( str, patt )
local t = {}
for s in str:gmatch("[^"..patt.."]+") do
t[#t+1] = s
end
return t
end
This is how the ComputerCraft devs do it in the shell program.local function runLine( _sLine ) local tWords = {} for match in string.gmatch( _sLine, "[^ \t]+" ) do table.insert( tWords, match ) end local sCommand = tWords[1] if sCommand then return run( sCommand, unpack( tWords, 2 ) ) end return false end
This is how i'd do it, much more efficient split functionTry this:local function split(strI,regEX) str = string.gsub(strI, regEX, " ") str = str .. " " local last = 1 local strTable = {} for i=1, str:len() do if str:sub(i,i) == " " then local subStr = str:sub(last, i - 1) table.insert(strTable, subStr) last = i + 1 end end return strTable end shell.run(unpack(split(read()," ")))
local function split( str, patt ) local t = {} for s in str:gmatch("[^"..patt.."]+") do t[#t+1] = s end return t end
Maybe ipairs()? Order of table elements is undefined when using pairs(). But ipairs() gets tArgs[1], then [2] and so on integer indexes until it gets nil value.I really dont get what you are asking, but if want to catch arguments when you run the program:local tArgs = { ... } for k, v in pairs( tArgs ) do print( v ) end
Its pretty much the same, since that table has only indexed keys. And to add to that, ipairs is pretty much deprecated. At least, that is what me is toldMaybe ipairs()? Order of table elements is undefined when using pairs(). But ipairs() gets tArgs[1], then [2] and so on integer indexes until it gets nil value.I really dont get what you are asking, but if want to catch arguments when you run the program:local tArgs = { ... } for k, v in pairs( tArgs ) do print( v ) end
It doesn't matter, in both pairs and ipairs all indexed values are returned in order, it is just the keyed values that return in a different order in pairs. Don't believe me, test it.Maybe ipairs()? Order of table elements is undefined when using pairs(). But ipairs() gets tArgs[1], then [2] and so on integer indexes until it gets nil value.
local t = {1,7,5,7,4,3,7,9}
for k,v in pairs(t) do
print('Index: '..k..' Value: '..v)
end