Jump to content




Divide String After First Space


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

#1 figgycity50

  • Members
  • 100 posts

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("[email protected]"..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:

#2 Yevano

  • Members
  • 376 posts
  • LocationUSA

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.

#3 figgycity50

  • Members
  • 100 posts

Posted 27 October 2013 - 02:00 PM

View PostYevano, on 27 October 2013 - 01:55 PM, said:

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.

#4 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

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

#5 figgycity50

  • Members
  • 100 posts

Posted 27 October 2013 - 02:09 PM

View PostKingOfNoobs, on 27 October 2013 - 02:04 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users