Send commands from a text file

Posted by Fletchling on Tue 24 Oct 2006 12:07 PM — 4 posts, 17,445 views.

Australia #0
I have an urge for the following;

1. Move a bunch of commands, such as speedwalks for instance, to a text file. Each command is prefixed with a self evident tag, separated by the command by a comma.

the text file, in this case is sw.txt, contains as the first few lines;

sw.txt
questor,run 8s
verume,run 2s17e2s6en15e21s2e
jov,run 2s17e2s6en15e21s2e
delf,run 4ne
delf,op n
delf,run 2nw32n8e12neu

2. I then want to be able to enter an alias, such as the alias below,
to load the text file,
interrogate each line,
check for a match for the text up to the first comma,
and if a match on that line, then
send the text following the comma to the mud as a command.

<aliases>
<alias
match="go *"
enabled="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>for line in io.lines ("sw.txt") do
if "%1" == (string.sub (line,1,(string.find (line, ",")-1))) then
Send (string.sub (line,(string.find (line, ",")+1)))
end -- if
end -- for
</send>
</alias>
</aliases>

Now the amusing thing is, it works just fine to a point. If I execute the command "go questor" it matches the right line, then sends the text to the mud (in this case a speedwalk) and finally I see this error message;

[string "Alias: "]:2: attempt to perform arithmetic on a nil value
stack traceback:
[string "Alias: "]:2: in main chunk

I perform some arithmatic on the string.find function to get rid of the comma from the match and the command. I kinda figure some aspect of the string function is objecting to my brutalisation, fair enough, but it still works to the mud.

Two questions.
1. How to do this without the error as it rather spoils the woohoo factor.
2. You may notice I have a few lines for the same match, that's a workaround to a sequence I want where I can't send the command with a command seperator (as in Send to Execute) so I have to split the command in multiple lines.

One purpose for this is that with more aliases and commands than my age enfeebled memory can handle, I can have bunch of aliases for the same purpose, different spellings for the same alias, amend/add/otherwisedit the commands in a simple file easily. Another pupose is this is a component for my mud.whereis + mud.whereamis and mud.takemethere scripts. Oh, I share the text files and other mud related stuff on my sharepoint server so my lifelong mud partner and I only need one file for each of the things we discover etc.

As always, your help is greatly appreciated. If I've left anything out, poke me for the missing bits.

Fletchling, aka these days, Cuddlepie in aardwolf
Australia #1
Arrgh, should have mentioned I'm on Mushclient 3.80.
Australia Forum Administrator #2
Quote:

1. How to do this without the error as it rather spoils the woohoo factor.


Your example actually worked for me without errors, but I guessed you had added a blank line to the end of your file, and sure enough, if you have a blank line it gives the error message.

The only bit of arithmetic is adding 1 to the column number of the comma, and if it doesn't find a comma the column number is nil.

Quote:

I can't send the command with a command seperator (as in Send to Execute) so I have to split the command in multiple lines.


Why is that exactly? The solution below seems to handle that.

Try this instead for the script:


for line in io.lines ("sw.txt") do
  tag, command = string.match (line, "^(.-),(.+)")
  if tag == "%1" then
    Execute (command)
  end -- matching tag
end -- for


It is neater to use string.match to break up the line into two pieces - the tag, and the command to be sent.

If the line does not have a comma then the string.match will return nil, which will not match "%1" and thus the line is ignored.

Then I send the command to Execute which will handle anything you like inside the command (eg. speedwalks, command seperators, etc.).

The first part of the regular expression is "(.-)" which matches the smallest number of characters it can, up to the comma, so extra commas inside the command will not matter.
Australia #3
You know, the bit I hate about posting a question is that it's a mark of defeat and public expression of how not very bright I am as you answer so quickly.

There was no blank line at the end, but I did have a line without a comma, removing that fixed the error. I'll rewrite the alias to Execute as you suggest.

Awesome support, thanks Nick.