[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Help with "|"
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Help with "|"

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by Bummer   (14 posts)  [Biography] bio
Date Tue 22 Dec 2009 08:14 PM (UTC)
Message
Hello there!

I have been using MUSH Client (THE GOD OF ALL CLIENTS) since I started mudding again this year (took about 10 years off!). NICK GAMMON, you are excellent. Thank you for ALL THAT YOU DO. It is just great. I look to the boards here often for information, and notice you always seem to lend a hand with MUDDING more than with your software. THANK YOU. OK, on with it...


What I would like to do is make a simple trigger that would welcome certain people as they pass. For example, I can easily make multiple triggers that would match on 'Name is here.' and then my send would be 'welcome Name'. Yet I know that this is sloppy and I would like to use the "|" instead.

How do I properly use the " | " so that I only need one trigger and it will match say "Name" and "Joey" AND "Bubba" and then will send "welcome %1 (or whom ever it should welcome)"

I believe it MUST be a regualr expression, and yet I cannot seem to make it work.

Thank you,
Bummer
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #1 on Tue 22 Dec 2009 08:23 PM (UTC)
Message
A regexp that should work might look like this:
^(Name|Joey|Bubba) is here\.$


However, to do any name at all, you might want this:
^(\w+) is here\.$

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Bummer   (14 posts)  [Biography] bio
Date Reply #2 on Tue 22 Dec 2009 08:32 PM (UTC)
Message
After toying with your response, maybe I need to use the circumflex inside the script send of the trigger, not inside the match. Here is what I am working with, and it does not work. I believe I am asking the script to do something that just doesn't work.


<triggers>
<trigger
enabled="y"
group="welcome"
match="^(.*?) is here\.$"
regexp="y"
send_to="12"
sequence="100"
>
<send>If %1 == x Then
Send ("welcome %1")
x == (Bubba|Nate)
end -- if</send>
</trigger>
</triggers>



Thank you, Twisol, for your fast response!

Cheers
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #3 on Tue 22 Dec 2009 08:38 PM (UTC)
Message
Assuming this is Lua you are using for the scripting language, you need to make 'if' and 'then' lowercase. Two, %1 needs to be quoted: "%1". Three, Lua has no concept of regular expressions natively, so "x == (Bubba|Nate)" makes no sense to it. Finally, it's always better to use a more specific character class than . (which means any character), if you can. Since you seem to be matching first names, \w+ should suffice.

Here's a somewhat fixed up version:

<triggers>
  <trigger
   enabled="y"
   group="welcome"
   match="^(\w+) is here\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>if "%1" == "Bubba" or "%1" == "Nate" then
  Send("welcome %1")
end</send>
  </trigger>
</triggers>

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Bummer   (14 posts)  [Biography] bio
Date Reply #4 on Tue 22 Dec 2009 09:55 PM (UTC)
Message
Great! I understand what you mean. That works!

I would prefer to put the names into some sort of variable or table, so I could easily add or remove names. It seems cumbersome to have the name within the script of the trigger, as I would have to edit the trigger by hand to add names. So... How could I use a variable or table to track the names?




Thank you SO much.

Bummer
[Go to top] top

Posted by Rakon   USA  (123 posts)  [Biography] bio
Date Reply #5 on Tue 22 Dec 2009 09:58 PM (UTC)
Message
Quote:

How do I properly use the " | " so that I only need one trigger and it will match say "Name" and "Joey" AND "Bubba" and then will send "welcome %1 (or whom ever it should welcome)"


Twisol is correct, use a regular expression for matching on such a trigger:

<triggers>
  <trigger
   enabled="y"
   group="welcome"
   match="^(\w+) is here\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>if "%1" == "Bubba" or "%1" == "Nate" then
  Send("welcome %1")
end</send>
  </trigger>
</triggers>


The (\w+) in the 'match' line , is RegEx that tells the trigger parser to 'match on any word, but not a number or boundary'.

However, if you're trying to say, match a list of known names and color those in output (such as for a friends list), you could make it easier. By using a variable to hold a list of names (separated by a "|") and then a trigger to match on that variable.


<triggers>
  <trigger
   custom_colour="17"
   enabled="y"
   expand_variables="y"
   keep_evaluating="y"
   match="\b(@!friendlist)\b"
   regexp="y"
   repeat="y"
   sequence="200"
   other_text_colour="mediumseagreen"
  >
  </trigger>
</triggers>




<variables>
  <variable name="friendlist">Friend1|Friend2|
Friend3</variable>
</variables>



Yes, I am a criminal.
My crime is that of curiosity.
My crime is that of judging people by what they say and think, not what they look like.
My crime is that of outsmarting you, something that you will never forgive me for.
[Go to top] top

Posted by Bummer   (14 posts)  [Biography] bio
Date Reply #6 on Tue 22 Dec 2009 10:02 PM (UTC)
Message
You all are great. I cannot thank you enough. I should have posted my questions months ago. My Lua and Regular expressions are going to the next level.


I see how you put the expanded variable into the MATCH. That will do me WONDERS.



I love you






=D
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #7 on Tue 22 Dec 2009 10:08 PM (UTC)
Message
Rakon is correct; the only annoying part is writing aliases you can use to easily manage the list. But, something like this would work.

You'd want two aliases, addwelcome and delwelcome. Both take a single name as a parameter. addwelcome would do something like this:

-- make sure the table exists if it doesn't already
welcome_names = welcome_names or {}

-- check if the name is already in the list
if welcome_names["%1"] then
  return
end

-- if it's not in the list yet, lets add it
table.insert(welcome_names, "%1")
-- we'll also add it this way, too, so we can easily check if it's in the list.
-- we set it to the length of the table, because that's the numeric index that the name is at. we can use that to remove it later.
welcome_names["%1"] = #welcome_names

-- lastly, lets update the MUSHclient variable that the regexp uses
local str = ""
for _,v in ipairs(welcome_names) do
  str = str .. v
end

SetVariable("friendlist", str)


The 'delwelcome' alias would do this:

-- make sure the table exists if it doesn't already
welcome_names = welcome_names or {}

-- check if the name isn't already in the list
if not welcome_names["%1"] then
  return
end

-- remove it if it's in the list
-- remember, the list has a name,index entry too; we're getting the index here and removing both entries
table.remove(welcome_names, welcome_names["%1"])
welcome_names["%1"] = nil

-- lastly, lets update the MUSHclient variable that the regexp uses
local str = ""
for _,v in ipairs(welcome_names) do
  str = str .. v
end

SetVariable("friendlist", str)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Bummer   (14 posts)  [Biography] bio
Date Reply #8 on Wed 23 Dec 2009 06:38 PM (UTC)
Message
Thank you, thank you, thank you all.


I have been reading up and trying to understand, yet the part below, has me boggled. Can someone possibly explain what is going on with this portion? The rest I understand....



welcome_names["%1"] = #welcome_names
The '#' is used to get the total number of keys within the table, yet what is this whole line actually doing?



-- lastly, lets update the MUSHclient variable that the regexp uses
local str = ""
for _,v in ipairs(welcome_names) do
str = str .. v
end
I see what this does, yet I do not understand the 'local' or the 'str'. Is 'str' a function or a variable you are creating?

The last part is really confusing. I need to read and understand more on functions and 'local' I believe, as they baffle me.

Thanks you,

Bummer
[Go to top] top

Posted by Rakon   USA  (123 posts)  [Biography] bio
Date Reply #9 on Wed 23 Dec 2009 06:56 PM (UTC)

Amended on Wed 23 Dec 2009 06:57 PM (UTC) by Rakon

Message
I can't help you with the first question, regarding the '#welcome_names' because I'm unsure exactly what that line does, myself. I use Python, not Lua; maybe Twisol can answer the first question.

Quote:

-- lastly, lets update the MUSHclient variable that the regexp uses
local str = ""
for _,v in ipairs(welcome_names) do
str = str .. v
end
I see what this does, yet I do not understand the 'local' or the 'str'. Is 'str' a function or a variable you are creating?

The last part is really confusing. I need to read and understand more on functions and 'local' I believe, as they baffle me.



This part, is a bit easier for me to explain.
local is just a variable scope declaration. It tells the Lua interpreter that the following variable is local only, to that function. in this case, 'str' is the variable , though at first I thought it was a typedef... then I realized Lua doesn't use typedeffing.

In the scope of THAT function, str is a local variable only used by the function. This is typically a way to prevent a variable name from overriding another namespace, or global variables. In many scripting and programming languages, it is NOT advisable to use builtin types/variables as names for YOUR variables, because it leads to errors or confusion on the maintainers part. It would be better to use a variable name that actually tells the programmer/maintainer what the variable holds/stands for.

I would just change str to another variable name in the example:

local str_names = ""
for _,v in ipairs(welcome_names) do
str_names = str_names .. v
end





Yes, I am a criminal.
My crime is that of curiosity.
My crime is that of judging people by what they say and think, not what they look like.
My crime is that of outsmarting you, something that you will never forgive me for.
[Go to top] top

Posted by Nick Gammon   Australia  (21,388 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Wed 23 Dec 2009 07:20 PM (UTC)
Message
Bummer said:

welcome_names["%1"] = #welcome_names
The '#' is used to get the total number of keys within the table, yet what is this whole line actually doing?


What Twisol has done in his suggested code is keep each name twice. So for example, after adding Nick to an empty table you would see:


welcome_names [1] = "Nick"
welcome_names ["Nick"] = 1


And then if you add Rakon you get:


welcome_names [1] = "Nick"
welcome_names [2] = "Rakon"
welcome_names ["Nick"] = 1
welcome_names ["Rakon"] = 2


The purpose of that, I think, is so you can quickly check if someone is there. For example to see if Nick is there you index into the table, eg.


if welcome_names ["Nick"] then
  -- Nick is here
end


As for the for loop, he is converting the table of names into a single string for use in an trigger, but I think he overlooked the "|" symbol.

And yes, "local" just makes a local variable rather than one in the global table.

I would probably write:


local str = ""
for _,v in ipairs(welcome_names) do
  if str ~= "" then
    str = str .. "|"
  end
  str = str .. v 
end
SetVariable("friendlist", str)


However, far more simply, you can use table.concat. So, instead of the whole "for" loop, you can just do this:


SetVariable("friendlist", table.concat (welcome_names, "|"))


That joins together the numerically-keyed part of the table with the "|" between each one (but not after the last, which is what you want) without using any extra variables or loops.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,388 posts)  [Biography] bio   Forum Administrator
Date Reply #11 on Wed 23 Dec 2009 07:25 PM (UTC)

Amended on Wed 23 Dec 2009 07:27 PM (UTC) by Nick Gammon

Message
But if you want to programmaticly check for names, and not automatically in a trigger, then a simple table is all you need. eg.

To add a name:


welcome_names = welcome_names or {}
welcome_names [string.lower ("%1")] = true  -- name now in list


To remove a name:


welcome_names [string.lower ("%1")] = nil  -- remove from list


And to check names:


if welcome_names [string.lower ("%1")] then
  -- name in list
end -- if


I put in the string.lower just in case names are sometimes capitalized and sometimes not.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Rakon   USA  (123 posts)  [Biography] bio
Date Reply #12 on Wed 23 Dec 2009 07:46 PM (UTC)
Message
Nick Gammon said:

But if you want to programmaticly check for names, and not automatically in a trigger, then a simple table is all you need. eg.

To add a name:


welcome_names = welcome_names or {}
welcome_names [string.lower ("%1")] = true  -- name now in list


To remove a name:


welcome_names [string.lower ("%1")] = nil  -- remove from list


And to check names:


if welcome_names [string.lower ("%1")] then
  -- name in list
end -- if


I put in the string.lower just in case names are sometimes capitalized and sometimes not.




This is the way I do it for my lists of names that I want highlighted. Only difference is my code is in Python, but the concepts are the same.

Yes, I am a criminal.
My crime is that of curiosity.
My crime is that of judging people by what they say and think, not what they look like.
My crime is that of outsmarting you, something that you will never forgive me for.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #13 on Wed 23 Dec 2009 07:59 PM (UTC)
Message
In cases where you can't trigger precisely on lines with the data you want to match - say, when you want to do something on every instance of a name rather than just on enter/exit, the trigger variable is invaluable. In this case though, yeah, just using a table might be easier. (Of course, if you want to highlight the names, it's easier by far to use a trigger variable)

Right, I'm double-keying the table to make it easy to (a) check and remove a name (with the name keys), and also easy to turn into a trigger variable string (with the integer keys). Nick's right... Somehow I forgot the all-important | character -and- table.concat. D'oh!


'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Bummer   (14 posts)  [Biography] bio
Date Reply #14 on Wed 23 Dec 2009 08:55 PM (UTC)
Message
I am going to work on this a bit and see where I get. Just wanted to post "THANK YOU ALL, TWISOL, RAKON and NICK GAMMON"



Merry Christmas and Happy Holidays!

Bummer
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


10,199 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]