Make alias

Posted by Halig on Wed 04 Feb 2015 02:39 PM — 29 posts, 101,761 views.

Portugal #0
Hi to all. I have a question, i've been killing my mind to try to figure this out, and i can't.
I have an alias, like this:
-stringo -(.*) -(.*) -(.*) -(.*)

What it does is the following:

var item;
var newitem;
var short;
var long;

item = "%1";
newitem = "%2";
short = "%3";
long = "%4";

world.send("string obj " + item + " name " + newitem);
world.send("string obj " + newitem + " short " + short);
world.send("string obj " + newitem + " long " + long);

My problem is that if the name (newitem) has 2 or more words, the code gives an error.
Example: The newitem will have the name mighty hammer gods.
When it does the short description on it, it gets the full name, mighty hammer gods, and not only one of the names. What i need is a line of code, that if it has one keyword, it uses that keyword, if it has 2 keywords, it sets the name to those 2 keywords, but when doing the short or the long, it uses only one of those keywords. And so on, if it has 3 the same thing.
What am i doing wrong?
Thank you.
Australia Forum Administrator #1
It would help to post the exact alias, and the exact text that causes it to fail.

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


However I am guessing you need to make the wildcards "non greedy" by adding a ?, like this:


 -stringo -(.*?) -(.*?) -(.*?) -(.*?)
Portugal #2
<aliases>
<alias
match="-stringo -(.*?) -(.*?) -(.*?) -(.*?)"
enabled="y"
expand_variables="y"
group="Restring"
regexp="y"
send_to="12"
keep_evaluating="y"
sequence="100"
>
<send>var item;
var newitem;
var short;
var long;

item = "%1";
newitem = "%2";
short = "%3";
long = "%4";

world.send("string obj " + item + " name " + newitem);
world.send("string obj " + newitem + " short " + short);
world.send("string obj " + newitem + " long " + long);</send>
</alias>
</aliases>

It is used for restring objects on the MUD. I don't know if all MUD's are alike.
The error is that on the second world.send, the newitem can only have one word, and not more then one word. If i name the item like i sayd before, the string saved on the var new item will be the all name, and i only need one, the first word for example.
Australia Forum Administrator #3
Can you please post an example of the MUD output that this should be matching on? Then I can test it.
Portugal #4
N:Reorx Vnum:1205 Incog: Wizi:
-stingo -book -book pedro -book pedro -Here lies a book
Huh?

N:Reorx Vnum:1205 Incog: Wizi:
string obj book name book pedro
string obj [b]book pedro[/b] short book pedro
string obj [b]book pedro[/b] long Here lies a book.

N:Reorx Vnum:1205 Incog: Wizi: Syntax:
string char <name> <field> <string>
fields: name short long desc title spec racetext
string obj <name> <field> <string>
fields: name short long extended

N:Reorx Vnum:1205 Incog: Wizi: Syntax:
string char <name> <field> <string>
fields: name short long desc title spec racetext
string obj <name> <field> <string>
fields: name short long extended

N:Reorx Vnum:1205 Incog: Wizi:


This is the MUD output. The two last outputs are the error, that's why i can't do what it suposed to do.
Thank you Nick.
Amended on Thu 05 Feb 2015 07:08 AM by Halig
Australia Forum Administrator #5
Nothing in your last post has "stringo" in it.


 match="-stringo -(.*?) -(.*?) -(.*?) -(.*?)"
Portugal #6
Nick,

I have posted all the outputs. I don't know what to do more :(.

The commands on the MUD are as followed:

string obj (object) name (new name)
string obj (new name) short (short description)
string obj (new name) long (long description)

These are the commands that are inputed on the MUD, so you can change the name of the object. The () are there only to show that it is what you need to input.
So i need to make an alias to do those 3 commands, in Jscript or with something else. See the (new name) that's where my problem is. If you set the new name with one word, there is no problem, but if you put 2 words on the new name, the second command don't work cause the (new name) must be entered on only one word. If i do those 3 commands, separated, by hand, they work fine.
USA #7
He's trying to match that to his input rather than mud output Nick. At least I'm pretty sure that's what I'm reading in his next to last post.


Halig, try putting your newitem output in quotes if it contains multiple words. What codebase is this for? Would be a good bit easier to help you with that info.

ex: string obj "(multi-word new name)" short (short description)
Amended on Thu 05 Feb 2015 08:48 AM by Meerclar
Portugal #8
Hi Meerclar,

You are right. I'm trying to match my input, not the MUD's. Thank you for that.

The codebase is based on Merc 2.1, based on Rom2.4 copyright 1993 - 1994 Russ Taylor.

It worked on what you said, put the "".
But on the script it doesn't work. How do i make an alias that makes all the three commands?
USA #9
If the server doesn't complain about the " " enclosure, change your alias to put keywords in quotes so that it doesn't 'confuse' the server with multiple keywords. That's why it's failing the way you set it up initially - the server is looking for a single 'word' for keyword when you restring and it should treat "(stuff stuff stuffy stuff)" as a single 'word' and allow you to use the alias like you're trying to do. If it's still complaining with the " " enclosure, you're going to need to rework the alias to properly fit the expected input pattern which could get very tricky if you've got something that was built with a lot of keywords.
Portugal #10
Thank you again.
The alias still getting messed up. Can you help me with the rewritten of that?
I'm really stuck, cause it works if you type one command at a time, but i want to automated that, make the three commands on one alias.
USA #11
Be sure expand variables is checked or this is guaranteed to fail spectacularly.

<aliases>
<alias
match="-stringo -(.*?) -(.*?) -(.*?) -(.*?)"
enabled="y"
expand_variables="y"
group="Restring"
regexp="y"
send_to="12"
keep_evaluating="y"
sequence="100"
>
<send>var item;
var newitem;
var short;
var long;

item = "%1";
newitem = "%2";
short = "%3";
long = "%4";

world.execute(string obj @item name @newitem);
world.execute(string obj "@newitem" short @short);
world.execute(string obj "@newitem" long @long);</send>
</alias>
</aliases>


I'm pretty sure I got that right at least. Using execute rather than send should keep the expanding variables functioning properly within the quotes and if not, Nick should spot any mistakes I made fairly easily. This one's a little beyond what I normally do with aliasing and the command syntax for string is definitely complicating things.
Amended on Thu 05 Feb 2015 11:43 AM by Meerclar
Portugal #12
variable name is not defined. I changed it to item, and then the newitem is not defined also.
I'm doing this in JScript.
Australia Forum Administrator #13
I got better results with matching on:


^-stringo -(.*?) -(.*?) -(.*?) -(.*?)$


Example alias in Lua:


<aliases>
  <alias
   match="^-stringo -(.*?) -(.*?) -(.*?) -(.*?)$"
   enabled="y"
   expand_variables="y"
   group="Restring"
   regexp="y"
   send_to="12"
   keep_evaluating="y"
   sequence="100"
  >
  <send>
item = "%1";
newitem = "%2";
short = "%3";
long = "%4";

-- debugging
print ("item", item)
print ("newitem", newitem )
print ("short", short)
print ("long", long)

-- Lua uses .. instead of + to concatenate strings

Send("string obj " .. item .. " name " .. newitem);
Send("string obj " .. newitem .. " short " .. short);
Send("string obj " .. newitem .. " long " .. long);

</send>
  </alias>
</aliases>


Test:


-stringo -foo -bar -pedro -hello world


Output:


item foo
newitem bar
short pedro
long hello world

string obj foo name bar
string obj bar short pedro
string obj bar long hello world
Amended on Thu 05 Feb 2015 07:54 PM by Nick Gammon
Portugal #14
Thank you Nick. But what if you insert:

-stringo -foo -bar of strenght -pedro -hello world

item foo
newitem bar of strenght
short pedro
long hello world

string obj foo name bar
string obj bar of strenght short pedro
string obj bar long hello world

The second line is where my problem resides. It can't be bar of strenght, but only bar.
USA #15
That still leaves an issue if he wants to do something like

-stringo -foo -chocolate bar -pedro -hello world

Passing string obj chocolate bar short pedro is going to cause the command to fail because the syntax will be 'wrong' despite the info being accurate. Is there a simple way to put the newitem info into quotes when its passed? I'm thoroughly out of my area of expertise on that issue.


Actually, it just occurred to me that I've been trying to do that the hard way. Halig, forget about trying to put quotes around the newitem variable when it's passed to the server, put quotes around your keywords if you're going to use multiple keywords. The quotes will get passed as part of the variable and the server should treat the entire thing as 1 'word' for command syntax purposes.

ie -stringo -foo -"chocolate bar" -pedro -hello world
Australia Forum Administrator #16
It parses that OK:


item foo
newitem chocolate bar
short pedro
long hello world

string obj foo name chocolate bar
string obj chocolate bar short pedro
string obj chocolate bar long hello world


The hyphens delimit the different word groups. You could conceivably add the quotes in the alias, eg.


<aliases>
  <alias
   match="^-stringo -(.*?) -(.*?) -(.*?) -(.*?)$"
   enabled="y"
   expand_variables="y"
   group="Restring"
   regexp="y"
   send_to="12"
   keep_evaluating="y"
   sequence="100"
  >
  <send>
item = "%1";
newitem = "%2";
short = "%3";
long = "%4";

print ("item", item)
print ("newitem", newitem )
print ("short", short)
print ("long", long)

Send('string obj "' .. item .. '" name "' .. newitem .. '"');
Send('string obj "' .. newitem .. '" short "' .. short .. '"');
Send('string obj "' .. newitem .. '" long "' .. long .. '"');

</send>
  </alias>
</aliases>




Test:


-stringo -foo -chocolate bar -pedro -hello world


Output:


item foo
newitem chocolate bar
short pedro
long hello world

string obj "foo" name "chocolate bar"
string obj "chocolate bar" short "pedro"
string obj "chocolate bar" long "hello world"
Australia Forum Administrator #17
Probably a better regexp would be this:


^-stringo -([^-]+?) -([^-]+?) -([^-]+?) -([^-]+?)$


That's because '*' matches anything, including hyphens. Also you want "one or more" not "zero or more" in the brackets.

This variation matches "one or more" of "anything except hyphen" which is what you really want.
Portugal #18
Now it works. Thank you all.
There is only one problem. When you put that, the name on the nweitem appears with the "chocolate bar".
Is there any way to take off the ""?
USA #19
Got an example of that so I can be sure what you're talking about?
Portugal #20
item book
newitem book of pedro
short book of pedro
long Here lies the book of pedro.
string obj "book" name "book of pedro"
string obj "book of pedro" short "book of pedro"
string obj "book of pedro" long "Here lies the book of pedro."

If then you look at your inventory, the item book of pedro, has the "booke of pedro", meaning that the item got the name with the "".
USA #21
Only place you should need quotes around your stuff is multi-word newitem. Nothing else needs them for the alias to function properly.
Portugal #22
Still nothing.
I guess i just have to make three aliases to make those things. If i have the three separated, everything is good.
Thank you for trying to help.
Australia Forum Administrator #23
Please post exactly what you are typing, exactly what your alias is, and exactly what gets sent.

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
Portugal #24
Hello again.

Nick, let's try this diferently.
Tell me something, if i have a variable that countains let's say, book of pedro.
How can i use that variable but get only the first word?
That's all i need, to collect from the variable the first word.
Thank you.
Australia Forum Administrator #25

test = "book of pedro"

firstWord = string.match (test, "^(%a+)")

print (firstWord)



Prints:


book


Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.
Portugal #26
Hello again Nick,
Thank you for your patience, but how ca i do that using Jscript?
I have more scripts assigned to aliases and if i change the script of MushClient to lua, the other ones don't work.
Thank you again.
Australia Forum Administrator #27
I recommend you use Lua. If you don't want to, put the other aliases into a plugin. Plugins each have their own script language, so some plugins can use Jscript and others Lua.

There must be a way of find a single word in Jscript, I suggest Google.
Portugal #28
Thank you Nick. I didn't knew that you could turn all the other aliases and triggers into a plugin using diferent languages. Thank you. All is working now.