if statement not working

Posted by Shady Stranger on Wed 05 Jan 2011 04:15 AM — 7 posts, 23,769 views.

USA #0
This script ignores the if statement and automatically sends the command after it even when if statement is false.


<triggers>
  <trigger
   enabled="y"
   match="* say*, &quot;* ok*&quot;"
   send_to="12"
   sequence="100"
  >
  <send>local name = %1
local match = %3

if name == match then
  Send ("say Pass")
end -- if
</send>
  </trigger>
</triggers>


The above is obviously the whole trigger.


local name = %1
local match = %3

if name == match then
  Send ("say Pass")
end -- if


I would actually like for this script to Send ("say Fail") if name does not equal match but am not sure if I should use "else" or not.
Amended on Wed 05 Jan 2011 04:30 AM by Shady Stranger
Australia Forum Administrator #1
You have to quote strings. Otherwise it substitutes:


name = Nick


Since the variable "Nick" won't exist, it will be nil, and then the if test:


if name == match then


... will fail (it will test "if name == nil").

So you need:


local name = "%1"
local match = "%3"

USA #2
Thanks, that works perfect. Now another question. How do I get it to ignore case?


local name = "%1"
local match = "%3"

if match == name then
  Send ("say Pass")

else
  Send ("say Fail")

end


For example if Nick = %1 and nick = %3, how do I get that to be true?
Amended on Wed 05 Jan 2011 04:56 AM by Shady Stranger
Australia Forum Administrator #3
Force them both to be either upper or lower case. There are two ways of doing this, both functionally equivalent:


if match:lower () == name:lower () then
  -- whatever
end -- if

-- or:

if string.lower (match) == string.lower (name) then
  -- whatever
end -- if


USA #4
I'm sorry to keep asking more questions but these things keep popping up. I really do appreciate the help.


* say*, "* ok*"


The point of this is to perform a certain command whenever the first wildcard is the same as the the third. This works great thanks to the help above but I have encountered a problem where some of the lines may look like this:


*** Nick says, "nick ok"


How do I ignore the "*** " when it happens but still have the script function normally when "*** " isn't present?

Thanks a bunch for the help.
Australia Forum Administrator #5
First click on the "convert to regular expression" button. That will give you the same functionality but be a regexp instead.

Then at the start, replace the ^ it will have put there by:


^(?:\*\*\* )?


That is saying, look for 3 asterisks followed by a space, but the ? at the end makes it optional. The ?: inside the brackets make this a non-capturing group (that is, it won't become a wildcard).

Another syntax would be:


^(?:\*{3} ){0,1}


That is, 3 asterisks, and 0 to 1 lots of the whole group.
Amended on Wed 05 Jan 2011 06:01 AM by Nick Gammon
USA #6
Thanks Nick! You're a great help and teacher.