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

Check first character of a string?

Started by Mackan90096, 28 March 2015 - 02:40 PM
Mackan90096 #1
Posted 28 March 2015 - 03:40 PM
So, let's say I have a variable like so:

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
InDieTasten #2
Posted 28 March 2015 - 03:46 PM

if(string.sub(read(),1,1) == "/") then
--# don't do anything?
else
--# do something?
end

you can replace the read() with your local variable obviously ;)/>
Edited on 28 March 2015 - 02:45 PM
Mackan90096 #3
Posted 28 March 2015 - 03:54 PM
Spoiler

if(string.sub(read(),1,1) == "/") then
--# don't do anything?
else
--# do something?
end

you can replace the read() with your local variable obviously ;)/>

Well, I have this here

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

If I input /me or /nick it works, but not line 13..
Edited on 28 March 2015 - 02:55 PM
ItsRodrick #4
Posted 28 March 2015 - 04:03 PM
You can replace
string.sub(var,1,1)
to
var:sub(1,1)
Goof #5
Posted 28 March 2015 - 04:32 PM
Spoiler

if(string.sub(read(),1,1) == "/") then
--# don't do anything?
else
--# do something?
end

you can replace the read() with your local variable obviously ;)/>

Well, I have this here

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

If I input /me or /nick it works, but not line 13..

What are you inputting?
it wont work if you input either /me or /nick, since LUA handles code from Top-bottom.
Mackan90096 #6
Posted 28 March 2015 - 04:51 PM
To give you more context, I'm making a chat program, I want this to handle commands, I don't want it to anything if the string that's inputted by read() starts with a / unless it's a recognized command..
GopherAtl #7
Posted 28 March 2015 - 08:22 PM
I'd do a lua pattern match at the start, myself, with a pattern and code structure basically like this:


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 on 28 March 2015 - 07:25 PM
Mackan90096 #8
Posted 29 March 2015 - 10:55 AM
@GopherAtl

Hooray! That worked! I am forever thankful :D/>