Wildcards as Function Parameters

Posted by Terry on Sun 10 Feb 2008 08:22 PM — 4 posts, 17,470 views.

USA #0
I was just wondering how you could make it so a wildcard could be the parname in a function. I tried this, but it didn't work:
<alias
   match="^l(|ook) (n|ne|e|se|s|sw|w|nw|u|d|north|northeast|east|southeast|south|southwest|west|northwest|up|down)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
function dir2 ("%1")
if "%2" == "n" then
 dir = "north"
elseif "%2" == "ne" then
  dir = "northeast"
elseif "%2" == "e" then
  dir = "east"
elseif "%2" == "se" then
  dir = "southeast"
...
end -- function

Send (look, " ", dir)</send>
  </alias>

What I want to have happen is that you can type, for example, 'l n', and "look north" will be sent instead.
If someone could help me, that'd be great.

The reason why I want it as a function, is because I have three of these things that use the same directional thing.
After a while, it just gets really repetitive. :(
Amended on Sun 10 Feb 2008 08:44 PM by Nick Gammon
Australia Forum Administrator #1
What you are doing here is confusing formal and actual parameters. I am assuming you meant %2 in the function declaration, as that is the direction. Let's see how that would look, expanded out, if you typed "look north":


function dir2 ("north")
if "north" == "n" then
 dir = "north"
elseif "north" == "ne" then
  dir = "northeast"
elseif "north" == "e" then
  dir = "east"
elseif "north" == "se" then
  dir = "southeast"
...
end -- function


Having a string literal ("north") in the function declaration is wrong. What you really mean is this:


function dir2 (s)
if s == "n" then
 dir = "north"
elseif s == "ne" then
  dir = "northeast"
elseif s == "e" then
  dir = "east"
elseif s == "se" then
  dir = "southeast"
...


return dir  -- return the result
end -- function


However all that does is declare the function, it doesn't call it. Then you would need to have the last couple of lines like this:


Send (look, " ", dir2 ("%2"))


This example calls the function dir2, passing the current direction (eg. "north") as the argument. Inside the function you examine the argument and decide what to substitute.

I have to say though that I don't think this has made things much simpler - unless you are planning to translate directions in lots of places. In that case the dir2 function should be in the script file (not this particular alias) so it can be shared amongst all aliases.

Quote:


Send (look, " ", dir)



There is also a problem here. You haven't quoted "look" so it is treated as a variable. Thus this sends: nil north (because look is nil). You meant:


Send ("look ", dir)


I also want to point out there is a much simpler way of achieving this in Lua. Try this:


dir_table = {
  n = "north",
  s = "south",
  north = "north",
  south = "south",
  e = "east",
  se = "southeast",

--- and so on 
  }  -- end of table

Send ("look ", dir_table ["%2"])


This removes the need for a function at all. I have a table of directions (dir_table) and inside is an entry for each direction you might type (as the key) and the value for each one is the direction you actually want sent.
Amended on Sun 10 Feb 2008 08:58 PM by Nick Gammon
USA #2
Quote:
I have a table of directions (dir_table) and inside is an entry for each direction you might type (as the key) and the value for each one is the direction you actually want sent.

Is it already built into MUSHclient? Or would I have to make one too?
Amended on Sun 10 Feb 2008 09:09 PM by Terry
Australia Forum Administrator #3
I meant, in my example. You would need to make one, like the one shown above with the extra directions fleshed out.