replicating command patterns for a function

TinFoilMkIV
So I was attempting to create a function or script that uses a pattern the way commands do, however I'm having trouble figuring out how to handle the different parts of the input.

For example, the command pattern for something like 'tell #object# #text#' basically uses the 'tell' to specify the command and then checks for an object name and saves whatever text comes after.

I'm wondering if it's possible to set up a script or function that does something similar?

HegemonKhan
as far as I know, there's only two ways of GETTING INPUT from the person playing the game:

the 'get input' Script and the 'Command' Element

but otherwise...

----------

this is a bit beyond me, as much of your questions are about, laughs, but I think you need to use:

'regex' syntax expressions, and~or JS within them too , or just JS by itself.

Also, maybe you need to use~work with 'delegates' too:

http://docs.textadventures.co.uk/quest/ ... egate.html

---------

JS:

http://docs.textadventures.co.uk/quest/ ... cript.html

Regex:

http://docs.textadventures.co.uk/quest/ ... ching.html

Commands:

http://docs.textadventures.co.uk/quest/ ... mmand.html (read the 'pattern' section, says something about ~ 'simple syntax vs regex syntax'

Functions:

http://docs.textadventures.co.uk/quest/ ... ction.html

----------

hopefully, something in this post is of help... laughs.

jaynabonne
When you say "something similar", do you mean takes a string and breaks it up into (say) the object and text pieces? If so, then you can use the Populate function to break a string apart. (This is the function the core code uses to parse commands.)

Here is an example:

      pattern = "^tell (?<object>.*?) (?<text>.*?)$"
command = "tell apple hello"
name = "foo"
if (IsRegexMatch(pattern, command, name)) {
varlist = Populate(pattern, command, name)
msg (varlist)
} else {
msg ("Does not match")
}

Note it uses a real regex string, not the "#object#" stuff. As coded, this will print out:

Dictionary: object = apple;text = hello



It has parsed the regex match and extracted the values into a dictionary. I put the "IsRegexMatch" check ahead of it, because if the command doesn't actually match the pattern, then the game just errs out, which is not desirable generally. :) So you really need to be sure the string matches before calling Populate.

The "name" part is actually a cache ID of sorts, if I understand it correctly. I believe what happens is that if you pass the same value to Populate that you passed to IsRegexMatch, then it will reuse the internal regex setup it used for the IsRegexMatch (so a sort of optimization).

If that's not what you were looking for, then if you could specify what part of the command line parsing you'd like in a script, I can try again. :)

Populate doc: http://docs.textadventures.co.uk/quest/ ... ulate.html

TinFoilMkIV
Yea I've looked over those previously. The Regex matching is decent but its far too forgiving. ie: looking for a Regex match for 'option1' allows you to literally type "askfjld;-=34@option1fkjkjl43#!$^%$7" and still be considered a valid match. Granted it's pretty unlikely for that much gibberish to contain a valid match on accident, but still, I'd like to require a bit more accuracy.

The command pattern syntax is good, but unfortunately as far as I can figure out it seems to only be valid in context of an actual command object. Which is why I'm trying to look into how one would code something similar to the command pattern for handling input.

jaynabonne
You just need a better, more exact regex pattern. See these:

msg(IsRegexMatch("option1", "askfjld;-=34@option1fkjkjl43#!$^%$7"))

True
So... bad.

msg(IsRegexMatch("^option1$", " ad  option1xadf jiaf")) 

False
'^' says "match beginning of line" and "$" says "match end of line". So it doesn't match your excessive string because it doesn't see the sequence <beginning of line>option1<end of line>.

And, of course, the desired case

msg(IsRegexMatch("^option1$", "option1"))

True

If you look into regular expressions, you'll see why Alex used them. They're fantastic for matching varying string patterns to whatever level of preciseness you wish. It all depends on the pattern you use to match against.

TinFoilMkIV
First off sorry I missed your original post Jay, I must have been typing it up just after you posted and just double checked my post at the bottom of the thread after posting and somehow completely missed yours.

That info about the command patterns is interesting and I'm sure I'll be able to find a use for that down the road. I was actually looking more for how to imitate the pattern matching the command objects use outside of an actual command.

Your examples in the second one should be just about what I'm looking for, if not exactly what I was trying to get. I was having problems locating the syntax for that sorta thing. Is there a page or listing that references these in one place that I can refer to in the future?

jaynabonne
I did a quick google search, and nothing jumped out as being "here they are in a nutshell". But this might help get started.

http://www.andymatthews.net/read/2009/1 ... xpressions

This has some as well:

http://www.cheatography.com/davechild/c ... pressions/

TinFoilMkIV
thanks for the info, I'll take a look at those.

EDIT: yea this is more or less exactly the sort of information I was looking for, I was having the issue of not knowing what I needed to search for to find this.

jaynabonne
Here's one more. This has better (as in actual) examples:

https://www.icewarp.com/support/online_ ... 030104.htm

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums