[DGD] How to make a more powerful function call?

Greg Lewis glewis at eyesbeyond.com
Wed Aug 8 06:41:20 CEST 2001


On Tue, Aug 07, 2001 at 09:56:00PM -0400, Brandon Rush wrote:
> I was talking to another coder (not using DGD) who was telling me about
> a foreach() kfun/efun. It's called in a form like
> 
> foreach( var in *array) { block of code }.

This is present in MudOS I believe.  Based on foreach from Perl no doubt.

> What it does is step through every element in *array, assigning it to
> var and running the block of code. It would replace code similar to
> 
> for (i=0,i==sizeof(*array),i++) {block of code}
> 
> except instead of referring to array[i] you could refer to var which is
> a little cleaner and easier to read.
> I can make an afun which takes a mixed var and mixed *array argument,
> but I can't find a way to send execution back from the function, run
> {block of code}, then return to the function again so I can set var to
> the next value and run {block of code} again. If someone knows a way for
> me to pass {block of code} to the function too, that might do the
> trick--or like I said, any suggestions/advice on what I should be trying
> to do to make this work.

This is mostly syntactic sugar.  You can get pretty much the effect you
want with a macro something like:

#define FOREACH(s, a) for (int i = 0; i < sizeof(a); s = a[i++])

There are obvious problems with this (e.g. nesting), but something similar
will do the trick most times.  I can't recall if you can declare the loop
counter in the for statement in DGD, but I think not, so you'd need to
alter this macro and declare it previously.

HTH.
-- 
Greg Lewis                            Email : glewis at eyesbeyond.com
Eyes Beyond                           Phone : (801) 765 1887
Information Technology                Web   : http://www.eyesbeyond.com

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list