@Foreach Question

0 views
Skip to first unread message

Parham

unread,
Jul 3, 2008, 3:22:23 PM7/3/08
to tin...@googlegroups.com
Hi,
I had a question. This is the code of a very simple cellphone I created. Every child of generic_cellphone has a number, and this is how the code is supposed to work:
Go through all the children of generic_cellphone with @foreach, and if the number attribute on any of them equals the number typed after the call command (the command syntax being call *), say connected, else say cannot find the target specified.
Well, as you can see, there is one thing wrong with this algorithm. If the generic_cellphone has 100 children, then the fail message will be given 99 times. But how can I fix this?
And is there something like a break command, for example, suppose we have three cellphones and the second one matches. The @foreach should stop immediately, how can I do that one?
Thanks!
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com

William Young

unread,
Jul 3, 2008, 2:52:05 PM7/3/08
to tin...@googlegroups.com
The method I tend to lean on for phone-number code is a bit of a cheat, using the dbref of the target object as the core of the phone-number. So if your database doesn't have anything over dbref #9999? Then trying to dial Cell Phone Object #1966 would be the in-character phone number 555-1966 (and dialing Cell Phone Object #867 would be 555-0867). Obviously, if you're up in the five digit range for dbrefs (or higher) one can change the prefix (so it's 550-1966 or whatever is required).

That way, the code just checks to see if the target dbref has the Cell Phone Parent as a parent, allowing the call to go through if it works out.

Unfortunately, this doesn't solve the initial @foreach question.

T'Then...
William D. Young

Real Mail 42 Winter St, Bangor ME, 04401
E-Mail youngw...@hotmail.com



From: parh...@gmail.com
To: tin...@googlegroups.com
Subject: [TinyMUX] @Foreach Question
Date: Thu, 3 Jul 2008 22:52:23 +0330

Enter the Zune-A-Day Giveaway for your chance to win — day after day after day Enter Now!

Mark

unread,
Jul 3, 2008, 3:20:05 PM7/3/08
to tin...@googlegroups.com
On Thu, 2008-07-03 at 22:52 +0330, Parham wrote:
> Hi,
> I had a question. This is the code of a very simple cellphone I
> created. Every child of generic_cellphone has a number, and this is
> how the code is supposed to work:
> Go through all the children of generic_cellphone with @foreach, and if
> the number attribute on any of them equals the number typed after the
> call command (the command syntax being call *), say connected, else
> say cannot find the target specified.
> Well, as you can see, there is one thing wrong with this algorithm. If
> the generic_cellphone has 100 children, then the fail message will be
> given 99 times. But how can I fix this?
> And is there something like a break command, for example, suppose we
> have three cellphones and the second one matches. The @foreach should
> stop immediately, how can I do that one?
> Thanks!

You could use a recursive function call instead of the @foreach.

It have been a very long time since I did any tux coding, but the
principle would be to create a function that took as parameters:

1. The number you were looking for.

2. The list of child objects.

The function looks at the list of child objects. If it is empty, then
the number hasn't been found, so return #-1.

Otherwise, use the first() item in the list of child objects, and
match() on the number attribute. If this is found, the function returns
the object id.

If it isn't found, then the function calls itself with the number you're
looking for, and rest() of the list of objects.

There's some trickery needed so you don't always evaluate the rest()
part, but I don't remember this in enough detail to give you an example.


Another way of tackling this problem would be to build a list of numbers
from children, then do a single match on that, and use the index of the
match to get back to the child object id.


Hope that helps a bit,

Mark
(aka Mark Farrell @ ATS a long long time ago)


A Character

unread,
Jul 3, 2008, 4:53:41 PM7/3/08
to tin...@googlegroups.com

th lsearch(,ething,\[match(%0,get(##/phonenumber))\])

________________________________
> From: parh...@gmail.com
> To: tin...@googlegroups.com
> Subject: [TinyMUX] @Foreach Question
> Date: Thu, 3 Jul 2008 22:52:23 +0330
>

A Character

unread,
Jul 3, 2008, 4:56:47 PM7/3/08
to tin...@googlegroups.com


Apparently it stripped out something. lsearch(all or lsearch(player if it owns all the cell phones

Parham

unread,
Jul 4, 2008, 12:28:32 AM7/4/08
to tin...@googlegroups.com
Hello,
I believe that would be what I'm looking for, but there is no help for
lsearch, and I don't know the syntax, and when I put it in think, it gives
back the same thing to me. I mean this:
lsearch(all,ething,\[match(%0,get(##/phonenumber))\])
And this is the code I have put, which does nothing:
@va generic_cellphone=$call *:@dolist
[children(num(generic_cellphone))]=ifelse(lsearch(all,ething,\[match(%0,get(##/number))\]),say
works,say doesn't work)
Thanks.

---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com

A Character

unread,
Jul 4, 2008, 5:22:12 AM7/4/08
to tin...@googlegroups.com

Guess who is used to PennMUSH? That's right, ME!
You want 'help search()' for syntax and all that jazz.

And probably something like:
@va generic_cellphone=$CALL *:Th Pemit(%#,If(Search(all ething=\[match(%0,get(##/NUMBER))\]),Works,Doesn't work))

Parham

unread,
Jul 4, 2008, 7:58:37 AM7/4/08
to tin...@googlegroups.com
Hello,
I TH'ed each part and figured that this,
th If(Search(eval=match(1,get(##/NUMBER))),Works,Doesn't work))

works, while EThings doesn't, since it says there is no such thing. So I
think what you wanted to put was eval, which evaluates each object. But
still, although I know a cellphone with the number 1 exists, this piece of
code,
>@va generic_cellphone=$CALL:Th
>Pemit(%#,If(Search(eval=match(1,get(##/NUMBER))),Works,Doesn't work))
won't work. I've tried the \['s and \]'s, but they don't make a difference
either.


---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
----- Original Message -----
From: "A Character" <a_cha...@hotmail.com>
To: <tin...@googlegroups.com>
Sent: Friday, July 04, 2008 12:52 PM
Subject: [TinyMUX] Re: @Foreach Question

> works,say doesn't work)=

Reply all
Reply to author
Forward
0 new messages