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

split variable string gmatch

Started by grimofdoom, 21 December 2014 - 12:54 AM
grimofdoom #1
Posted 21 December 2014 - 01:54 AM
I have found, after a while of searching, that gmatch can be used to split a string of text by the spaces… I have been trying to use the lua interpreter to to this and i always come to the error:

lua:39: [string "lua"] :1:unexpected symbol

this is what i have set up

var="this is a string of characters"
print(var.gmatch(%a+)

(i have not found any useful lua help online either on this…i may be misunderstanding something)
my main goal of this was to take in a users input in a form of a command that minecraft uses(ex: /time set 0, /help 8) and then use the following split string pieces to produce the required output if possible.
KingofGamesYami #2
Posted 21 December 2014 - 03:53 AM
There are two problems with what you posted

#1) The pattern is a string
and
#2) You used incorrect syntax when calling gmatch


var = "bla bla bla"
print( var:gmatch( "%a+" ) )

Furthermore, gmatch returns a function which can be iterated through using a for loop, so it's going to say something like:

function: 0x5314c0


Here is a valid example of gmatch AND match (which I suspect you may want).


local str = read()
for word in str:gmatch( "%a+" ) do
  print( word )
end


print( "Enter name:age" )
local str = read()
local name, age = str:match( "(%a+):(%d+)"
print( name .. "is " .. age .. " years old!" )

If you have any questions, feel free to ask.

Edit: Forums putting stuff in my code I don't want there…
Edit 2: Disabled emoticons (hopefully this fixes it)
Edited on 21 December 2014 - 02:56 AM
grimofdoom #3
Posted 21 December 2014 - 04:11 AM
OK, i see now… and, what is the difference "%a+" and "%d+"? i tried searching about it too, and could not understand it.


EDIT
i tried your second example exactly, and i get the error "cannot concatenate nil and string"

I also could not figure out how to separate the phrase "/register johndoe" (with space instead of ":")

EDIT2
When using the "tostring()" on the parts, i get the outcome of "function:1374789d is nil old"
Edited on 21 December 2014 - 03:31 AM
KingofGamesYami #4
Posted 21 December 2014 - 04:40 AM

print( "Enter name:age" )
local str = read()
local name, age = str:match( "(%a+):(/>%d+)" ) --#fixed parenthese
print( name .. "is " .. age .. " years old!" )
When it asks for name:age you must give it a name without spaces, :, your age. Example:

KingofGamesYami:27
*note: I'm not actually 27
I also could not figure out how to separate the phrase "/register johndoe" (with space instead of ":")
%s is all space characters.

local str = read()
local name = str:match( "/regiseter%s(%a+)" )
print( "You registered " .. name )
grimofdoom #5
Posted 21 December 2014 - 04:55 AM
What i want to do is separate the "/register" and "nickname" as seperate variables and the variations of what ive seen so far on this thread has just been left to error. I did figure out an annoying way by making an array and using the "for word in var:gmatch("%a+")", but i believe there is a shorter way to remove the extra lines it adds.

i=1
str=read()
command={}
for word in str:gmatch("%a+") do
  command[i]=word
  i=i+1
end

print (command[1])
print (command[2])

there must be a much shorter way of doing this.(the reason i want to separate the /register and nickname is to have more options than /register, like /me or /pm…ect for a chatroom i am building)
MKlegoman357 #6
Posted 21 December 2014 - 09:46 AM
Your code is almost as short as it can be. If you wany to seperate the string by spaces you could use something like this:


local str = "hello world"
local words ={}

for word in str:gmatch("[^%S]+") do
  words[#words + 1] = word
end

for i, word in ipairs(words) do
  print(word)
end
Bomb Bloke #7
Posted 21 December 2014 - 09:57 AM
For what it's worth, when assigning values, you don't need to use multiple lines for multiple variables.

Eg:

i, str, command = 1, read(), {}
local str, words = "hello world", {}