[DGD] foreach

Noah Gibbs noah_gibbs at yahoo.com
Thu Sep 8 17:28:01 CEST 2005


  Minor point - you can extract all the values as well as all the keys into two
parallel arrays, then just use a single counter to iterate through the arrays. 
This will save you a line or two.

  Lee Salzman's old 'foreach' patch appears to no longer be available (not even
via the Wayback Machine), so I don't know exactly what to tell you.  You might
be able to construct a foreach-like macro (using the standard preprocessor)
which would save you some lines of typing, but I'm required on principle to
warn you away from that :-)

--- solicit <solicit at hot.ee> wrote:

> I am currently trying to port a mudlib from MudOS to DGD, and I've hit a
> major show-stopper - foreach. Just so you know, I've read the mailing list
> archives threads about it.
> 
> I can agree that sometimes it is better and clearer to use for() instead of
> foreach() when iterating over an array, but no so for a mapping. Consider
> this example:
> 
> With foreach:
> mapping m;
> m["a"] = "foo";
> m["b"] = "bar";
> foreach(key, value in m) {
>     printf("key: %s, value: %s\n", key, value);
> }
> 
> Without foreach:
> mapping m;
> string key, value; /* (if the mapping is declared/filled in somewhere else,
> then it can be hard to guess the type [although you could use mixed]) */
> string *keys;
> int i;
> 
> m["a"] = "foo";
> m["b"] = "bar";
> keys = keys(m);
> for(i = 0; i < sizeof(keys); i++) {
>     key = keys[i];
>     value = m[key];
>     printf("key: %s, value: %s\n", key, value);
> }
> 
> That's about twice as long (thrice if you don't count the
> filling-in-the-mapping part). Now consider me having to go through the 2300+
> instances of foreach() in my mudlib (easily over half of them critical), and
> changing them all. Not something I'd like to do.
> 
> Now, if someone happens to have the old patch for DGD that adds foreach, then
> you can just ignore all this, but I thought I'd present my argument first. :)
> 
> -----------------------------------------
> ITV - Sinu lemmiksaated internetis!
> http://www.itv.ee
> 
> __________________________________________
> http://mail.dworkin.nl/mailman/listinfo/dgd
> 




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the DGD mailing list