Re: #loop adjustments
Eugene <[email protected]> Thu, 10 Nov 2005 11:13:50 +0300
| Newsgroups | gmane.comp.games.mud.client.lyntin |
|---|---|
| Message-ID | <[email protected]> |
On Wed, 9 Nov 2005 19:29:09 -0600 (CST) will guaraldi <[email protected]> wrote: > >I totally forgot that I'm not the maintainer anymore. :p You are welcome :) >Anyhow... I just checked in a change to the #loop >command adding the ability to iterate over strings. So >it used to do: > > #loop {1,4} {say %0} > >which would execute: > > say 1 > say 2 > say 3 > say 4 > >Now it does that and can also do something like this: > > #loop {tom,harry,pete,fred} {say hi, %0.} range=false > >which would execute: > > say hi, tom. > say hi, harry. > say hi, pete. > say hi, fred. > >Someone mentioned adding something like this a while >back, but I forget the details including why I (and it >was probably me) negged the idea. I did wrote a module with the #for and #do commands some time ago, and it even was discussed here. But it wasn't put neither into the distribution, nor into the repository, I don't remember why. So, it's ok with the loop command, I guess. The module itself, just as an example, is in the attachment. -- Eugene --- Professional hosting for everyone - http://www.host.ru
for.py
(text/x-python, 1.4 KB)
from lyntin import exported, modules, utils
def for_cmd(ses, args, input):
"""
Implements "for" loop, with iteration list as single argument
(joined with SEP separator), followed by command.
#for for,do {#help %0}
#for 3k:arctic:wheel {#zap %0} sep=:
"""
do = args["do"]
count = 1
for item in args["arglist"].split(args["sep"]):
exported.lyntin_command(utils.expand_vars(do, {
'0': item,
'_': count+1,
}),
internal=1, session=ses)
def do_cmd(ses, args, input):
"""
Implements "do" loop, with command first and iteration list last.
Note variable length iteration list.
#do {#session %0} {3k 3k.org 3000} {arctic mud.arctic.org 2700}
"""
do = args["do"]
count = 1
for item in args["arglist"]:
vars = { '0': item, '_': count }
index = 1
for arg in item.split(' '):
vars[str(index)] = arg
index += 1
exported.lyntin_command(utils.expand_vars(do, vars),
internal=1, session=ses)
count += 1
commands_dict = {
'for': (for_cmd, "arglist do= sep=, quiet:boolean=false"),
'do': (do_cmd, "do arglist* quiet:boolean=false")
}
def load(): modules.modutils.load_commands(commands_dict)
def unload(): modules.modutils.unload_commands(commands_dict)