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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Send commands from a text file

Send commands from a text file

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


Posted by Fletchling   Australia  (54 posts)  [Biography] bio
Date Tue 24 Oct 2006 12:07 PM (UTC)
Message
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
[Go to top] top

Posted by Fletchling   Australia  (54 posts)  [Biography] bio
Date Reply #1 on Tue 24 Oct 2006 12:27 PM (UTC)
Message
Arrgh, should have mentioned I'm on Mushclient 3.80.
[Go to top] top

Posted by Nick Gammon   Australia  (22,990 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Tue 24 Oct 2006 08:48 PM (UTC)
Message
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.

- Nick Gammon

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

Posted by Fletchling   Australia  (54 posts)  [Biography] bio
Date Reply #3 on Wed 25 Oct 2006 06:28 AM (UTC)
Message
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.
[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.


11,981 views.

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 HostDash]