Forum

> > CS2D > Scripts > Another method to capture strings and parameters.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Another method to capture strings and parameters.

9 replies
To the start Previous 1 Next To the start

old Another method to capture strings and parameters.

mozilla1
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
parameter = {}
i = 0
	
for word in string.gmatch(txt, "pattern") do
	i = i + 1 		
	parameter[i] = word  	
end  	 	

for _, i in ipairs(parameter) do print(i) end
end

Hello people, tomorrow, i tried another way to catch strings, and I can't find a way to capture it properly.
"txt" is a global variable which represents what player typed.
"pattern" is a parameter to find some special strings, like alphanumerics, hexadecimals, numbers, etc.
The list of patterns are found here: http://www.lua.org/manual/5.1/manual.html#5.4.1

Results in "print(i)" with the input "/dir 1":

Spoiler >



I wanna a solution to catch a output with these values, when I type "/dir 1"
parameter[1] = /dir
parameter[2] = 1


Anyone has ideas?

old Re: Another method to capture strings and parameters.

EngiN33R
Moderator Off Offline

Quote
You may want to try using %S+ as a pattern, which is 1 or more occurrences of everything except for spaces - as specified in the manual, capitalizing a letter in a pattern will result in reversing the pattern, and I'm quite sure you intend to separate arguments with spaces.

My working code >

Also, starting chat commands with / won't work, since that symbol is reserved for parsing console commands from the chat, much to user oxytamine's regret.

old Re: Another method to capture strings and parameters.

Yates
Reviewer Off Offline

Quote
user EngiN33R has written
Also, starting chat commands with / won't work, since that symbol is reserved for parsing console commands from the chat, much to user oxytamine's regret.

And to add to that I also asked DC a while back and there is no way to get around it. There are also no plans to change it either.

old Re: Another method to capture strings and parameters.

mozilla1
User Off Offline

Quote
user EngiN33R has written
You may want to try using %S+ as a pattern, which is 1 or more occurrences of everything except for spaces - as specified in the manual, capitalizing a letter in a pattern will result in reversing the pattern, and I'm quite sure you intend to separate arguments with spaces.

My working code >

Also, starting chat commands with / won't work, since that symbol is reserved for parsing console commands from the chat, much to user oxytamine's regret.



Thanks alot. Never thought about capitalize the pattern %s+, I tried everything but I forgot this.

user oxytamine has written
Not really, I want to see /help and /kick commands instead of these terrible-looking @help or !kick.


Slash/Backslash (even in this example) always looks cool, but it is alerady used to handle RCon commands, too bad.

old Re: Another method to capture strings and parameters.

omg
User Off Offline

Quote
this code can be easily found in scripts
1
2
3
4
5
6
7
8
function totable(t,match)
	local cmd = {}
	if not match then match = "[^%s]+" end
	for word in string.gmatch(t, match) do
		table.insert(cmd, word)
	end 
	return cmd 
end
returns a table containing each word in the string
t is the string parameter
match is an optional search parameter. by default, it looks for everything except spaces (same as %S)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview