local inp = read()
and the user would enter "/hi".
How would I check if the first character of the string is a slash, (/) and if so, not do anything?
Thanks
- Mackan
Posted 28 March 2015 - 03:40 PM
local inp = read()
Posted 28 March 2015 - 03:46 PM
if(string.sub(read(),1,1) == "/") then --# don't do anything? else --# do something? end
Edited by InDieTasten, 28 March 2015 - 03:45 PM.
Posted 28 March 2015 - 03:54 PM
function input()
while true do
_, p1 = os.pullEvent("key")
if(p1 == 28) then
local inp = read()
if(string.sub(inp, 1,5) == "/nick") then
if(string.len(string.sub(inp, 6)) >= 1) then
local np = string.sub(inp, 7)
send(name.." is now known as "..np)
name = np
elseif(string.sub(inp, 1,3) == "/me") then
send("* "..name.." "..string.sub(inp, 4))
elseif(string.sub(inp, 1,1) == "/") then
end
else
send("<" .. name .. "> "..inp)
end
end
end
end
Edited by Mackan90096, 28 March 2015 - 03:55 PM.
Posted 28 March 2015 - 04:03 PM
string.sub(var,1,1)to
var:sub(1,1)
Posted 28 March 2015 - 04:32 PM
Mackan90096, on 28 March 2015 - 03:54 PM, said:
function input()
while true do
_, p1 = os.pullEvent("key")
if(p1 == 28) then
local inp = read()
if(string.sub(inp, 1,5) == "/nick") then
if(string.len(string.sub(inp, 6)) >= 1) then
local np = string.sub(inp, 7)
send(name.." is now known as "..np)
name = np
elseif(string.sub(inp, 1,3) == "/me") then
send("* "..name.." "..string.sub(inp, 4))
elseif(string.sub(inp, 1,1) == "/") then
end
else
send("<" .. name .. "> "..inp)
end
end
end
end
Posted 28 March 2015 - 04:51 PM
Posted 28 March 2015 - 08:22 PM
inp=read()
--//this checks if the line matches "/<cmd>", with optional arguments separated from the
--//command with a space. the ^ and $ just require the pattern to match the whole string,
--//from beginning to end
local cmd,args = imp:match("^/(%w+)%s*(.-)$")
if cmd then
--//cmd will only be "true" if the pattern matched, so here we know we have a command
if cmd=="nick" then
--//args is the new nickname
elseif cmd=="me" then
--//args is the text to pose
else
--unknown command, give error locally
end
else
--//didn't match the cmd pattern, just text to say, so send inp
end
Edited by GopherAtl, 28 March 2015 - 08:25 PM.
Posted 29 March 2015 - 09:55 AM
0 members, 2 guests, 0 anonymous users