No idea what i'm doing wrong

Posted by Yasik on Thu 01 Nov 2007 07:46 PM — 9 posts, 45,155 views.

Ukraine #0
Dont laugh too loud :)

I want to make an alias "kk", that does "kick elf" when i
type "kk elf" and does just "kick" when i type "kk".
So i made this:

Alias: ^kk (?P<who>.*?)$
checked Enabled
checked Regular Expression
Send To: World
Label: kick_someone
Script: Kick

The script itself looks like this:

sub Kick {
$kicked = GetAliasWildcard("kick_someone", "who");
if ($kicked eq "") { $world->Send("Kick"); }
else { $world->Send("Kick"." ".$kicked); }
}

It does "Kick elf" when i type "kk elf", but it sends just "kk" to the world when i type "kk" without arguments. I give up. Where's the bug(s), ex&#1089;ept in my DNA? :)

Also wonder how to bind a macros on Alt+W ...
USA #1
There's probably something in the string like a space or something that's preventing it from being equal to "". Or, the string is undefined. You can try:


sub Kick {
  $kicked = GetAliasWildcard("kick_someone", "who");
  if ($kicked == undef or $kicked ~= /^\s*$/) { $world->Send("Kick"); }
  else { $world->Send("Kick"." ".$kicked); }
}


The regular expression is saying: return true if kicked contains only space characters from the beginning to the end. (It might be =~, not ~=; I can never remember that.)
Ukraine #2
Both seems not to work for some reason...
In case of "~=" it shows:

Script error
World: Sloth 3
Execution of line 4 column 0
Immediate execution
syntax error at (eval 2) line 3, near "$kicked ~"
syntax error
Error context in script:
1 : sub Kick {
2 : $kicked = GetAliasWildcard("kick_someone", "who");
3 : if ($kicked == undef or $kicked ~= /^\s*$/) { $world->Send("Kick"); }
4*: else { $world->Send("Kick"." ".$kicked); }
5 : }

In case of "=~" it sends "Kick" when i type "kk elf" and "kk" when i type "kk".

I think i'm goin to read some perl manuals or sort...
Amended on Thu 01 Nov 2007 08:38 PM by Yasik
USA #3
Some languages use <> for symbolizing not equal to, so you might try that. I myself have not used perl for anything other than a simple assignment in cgi-bin for use on a webpage, and that was several years ago, so it is anyone's guess.

-Onoitsu2
USA #4
Perl uses != for not-equals, except for strings, where you use ne. (But we're not looking for not-equals here.)

Unfortunately Yasik I don't really know how to go forward from here since it might be a problem with how you're setting up the alias. (I don't really know how to do that.)
Australia Forum Administrator #5
Quote:

I want to make an alias "kk", that does "kick elf" when i
type "kk elf" and does just "kick" when i type "kk".


The simple approach is to not build in the space into the alias. That is:

Match: kk*
Send: kick%1

That way, if you type kk on its own, it still matches (as it isn't looking for a space), and sends "kick", but if you type "kk elf" then %1 is " elf" and it sends "kick elf".
Ukraine #6
It does work now :) Thanx all! Looks like i was diggin in too deep as for beginner

What about Alt-W macros? Dont want change habits
Australia Forum Administrator #7
In Lua, you could do this:

Accelerator ("Alt+W", "some_action_here")

In Perl the syntax will be slightly different, see this page for a description:

http://www.gammon.com.au/scripts/doc.php?function=Accelerator


Accelerators are temporary changes to the world, to make it permanent you need a script to run at world open, and do them in that.
Amended on Fri 02 Nov 2007 05:13 AM by Nick Gammon
#8
The problem was not with your original script, but with the trigger regex. The way it was built, it was looking for `kk maybesomething'. Notice the whitespace behind "kk"? Thats why simply "kk" didn't trigger at all and went through directly to the world. If you modify the regex to `^kk(?P<who>.*?)$' (or the simplest way as Nick suggested `^kk(.*?)$'), it would have worked right away, but possibly also catching things it is not supposed to catch (like "kkinfo", which might be some other alias). So a fool-proof version would be `^kk(\s.*)?$', and then send "kick %1" to the world.
[EDIT: fixed my regex, because it still "required" the whitespace to be there. I shouldn't post when tired..]

And Dave's example resulted in a script error, because `~=' is not a valid operator. `=~' is what he meant, which matches a regex on a string.