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

Divide String After First Space

Started by figgycity50, 27 October 2013 - 12:36 PM
figgycity50 #1
Posted 27 October 2013 - 01:36 PM
:(/> so i need help. i am making an alternative shell for computercraft. due to the stupid nature of shell.run() i need to split my command inputted to a 2 part array. For example, say i inputted this:

ls rom

I would have an array like this:

{'ls', 'rom'}

My code:


while true do
  if os.computerLabel() then
lbl = os.computerLabel()
  else
    lbl = os.computerID()
  end
  write("root@"..lbl.." $ "..dir)
  local command = read()
  if command == "exit" then
    break
  else
    local exists, path = findFile(command)
    if exists then
	  shell.run(path)
    else
	  print("Unknown command: "..command)
    end
  end
end

Any idead on how? :ph34r:/>
Yevano #2
Posted 27 October 2013 - 01:55 PM
shell.run can also be given a single parameter containing the entire command string. It's not documented on the wiki, but shell.run("ls rom") should work.
figgycity50 #3
Posted 27 October 2013 - 02:00 PM
shell.run can also be given a single parameter containing the entire command string. It's not documented on the wiki, but shell.run("ls rom") should work.
for some reason it works on the lua console but not in my program.
M4sh3dP0t4t03 #4
Posted 27 October 2013 - 02:04 PM
For splitting a string into multiple substrings I would use the following function
function splitString(str)
  local fields = {}
  str:gsub("([^ ]+)", function(c) table.insert(fields, c) end)
  return fields
end

If you would do splitString("Hello world!"), it would return the table {"Hello", "world!"}

And if you want to be able to split it by any other separator you should use this:
function splitString(sep, str)
  local sep, fields = sep or " ", {}
  local pattern = string.format("([^%s]+)", sep)
  str:gsub(pattern, function(c) table.insert(fields, c) end)
  return fields
end

Also, on this article there are a couple of other ways of splitting strings listed(also I have gotten this one from this article): http://lua-users.org/wiki/SplitJoin

Edit: I have just added a little bit of information
figgycity50 #5
Posted 27 October 2013 - 02:09 PM
For splitting a string into multiple substrings I would use the following function
function splitString(str)
  local fields = {}
  str:gsub("([^ ]+)", function(c) table.insert(fields, c) end)
  return fields
end

If you would do splitString("Hello world!"), it would return the table {"Hello", "world!"}

And if you want to be able to split it by any other separator you should use this:
function splitString(sep, str)
  local sep, fields = sep or " ", {}
  local pattern = string.format("([^%s]+)", sep)
  str:gsub(pattern, function(c) table.insert(fields, c) end)
  return fields
end

Also, on this article there are a couple of other ways of splitting strings listed(also I have gotten this one from this article): http://lua-users.org/wiki/SplitJoin

Edit: I have just added a little bit of information

Thanks! that's just what i need