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

string.gsub with 'special' symbols ( / ) [ } { ] =?!""#&¤/¤(&

Started by Goof, 16 December 2013 - 01:57 PM
Goof #1
Posted 16 December 2013 - 02:57 PM
Hello.

Im working on a sandbox machine, which listen for a command, split the command by the spaces ( therefore string.gsub ), but it has to keep the symbols like "/" og "\"….

Why doesn't it do that?
Spoiler

-- sandbox ADVLOCK --
local mainPath=".advlock/"
local data={
	["paths"]={
		_sandbox=mainPath..".system/sandbox";
		_system=mainPath..".system/";
	};
	["visible"]={
		_sandbox="sandbox";
	};

}
if not fs.isDir(mainPath..".system/") then
	fs.makeDir(mainPath..".system/")
end
if not fs.isDir(data["paths"]._sandbox) then
	fs.makeDir(data["paths"]._sandbox)
end
local promptColour, textColour, bgColour
if term.isColour() then
	warningColour = colors.red
	promptColour = colours.yellow
	textColour = colours.white
	bgColour = colours.black
else
	warningColour = colors.white
	promptColour = colours.white
	textColour = colours.white
	bgColour = colours.black
end
function curDir()
	local dictionary=shell.dir():gsub(".advlock/.system/","")
	return dictionary
end
	--shell.setDir(data["paths"]._system)
	shell.setDir(data["paths"]._sandbox)
	term.setBackgroundColor(bgColour)
	while true do
		term.setTextColor(promptColour)
		term.write(curDir().."> ")
		term.setTextColor(textColour)
		local command = "cd /"--read()
		print(command)
		local tbl = {}
		for w in string.gmatch(command,"%a+") do
			tbl[#tbl+1]=tostring(w)
		end

		print(textutils.serialize(tbl))
		term.setTextColor(promptColour)
		if command == "break" then
			break
		elseif command == "cd" then
			if curDir() == "sandbox" then
				term.setTextColour(warningColour)
				textutils.slowPrint("AdvLockSandBox: \nThis is the main dictionary!")
				term.setTextColor(promptColour)
			end
			shell.setDir(data["paths"]._sandbox)
			
		else
			shell.run(command)
		end
		break
	end
shell.setDir("")

And btw:
Does anyone know in which "folder" a sandbox is normally stored in?
like in .sys/.progs./.security/sandbox or something like that

Thanks in Advance
Edited on 16 December 2013 - 02:00 PM
Lyqyd #2
Posted 16 December 2013 - 03:28 PM
Read up on Lua patterns and how to escape the special pattern characters (like ".").
Goof #3
Posted 16 December 2013 - 03:37 PM
Oh thanks, First I didn't know how all the patterns worked, but now i seem to have an understanding of it..

But, it seems to return 1 single string, when it was supposed to make 2 different strings ( because of the space before the /)

I used this pattern:
"%a.+"

EDIT:
by using:
"." then I receive 5 different values in the "tbl" table, but then i have to convert everything into word form, by repeating a str=str..dat["thingyWord"][num+1]
Or?
Edited on 16 December 2013 - 02:42 PM
MKlegoman357 #4
Posted 16 December 2013 - 03:49 PM
If you want to split everything with patterns then use this pattern:


%S+

%s means all space characters. When you capitalize it the pattern then finds everything except space characters.
Goof #5
Posted 16 December 2013 - 03:57 PM
Ahhhh! Well that makes even more sense to me.
XD I just wasted 5 mins, writing this converter xD :D/>

local wordBreak=false
local strTbl={}
local str=""
for k,v in pairs(tbl) do
	--print(v)
	if v == " " then
		wordBreak=true
	elseif not wordBreak then
		--print(v)
		--print(v)
		str=str..tostring(v)
	elseif wordBreak then
		strTbl[#strTbl+1]=tostring(str)
		str=""..tostring(v)
		strTbl[#strTbl+1]=tostring(str)
		wordBreak=false
	end
end

xD

Thanks!
Edited on 16 December 2013 - 02:58 PM
MKlegoman357 #6
Posted 16 December 2013 - 04:23 PM
Ahhhh! Well that makes even more sense to me.
XD I just wasted 5 mins, writing this converter xD :D/>/>

local wordBreak=false
local strTbl={}
local str=""
for k,v in pairs(tbl) do
	--print(v)
	if v == " " then
		wordBreak=true
	elseif not wordBreak then
		--print(v)
		--print(v)
		str=str..tostring(v)
	elseif wordBreak then
		strTbl[#strTbl+1]=tostring(str)
		str=""..tostring(v)
		strTbl[#strTbl+1]=tostring(str)
		wordBreak=false
	end
end

xD

Thanks!

Your welcome! :D/>

And don't forget to read my signature :P/>
Lyqyd #7
Posted 16 December 2013 - 05:01 PM
Don't beg for votes in signatures.

Gonna have to add that bit of common sense to the forum guidelines, it seems.