digitalmars.D - Block thinking, and the initialization of variables
- Christopher Stevenson (23/23) Aug 15 2004 I tried this foreach statement a little while back to neatly format outp...
- Nick (10/23) Aug 15 2004 I believe this is expected behavior. Every time you "enter" the {} code ...
- Deja Augustine (5/29) Aug 15 2004 While I have yet to implement foreach in D.NET, from looking at the comp...
- Russ Lewis (5/39) Aug 16 2004 I can't speak for how the compiler does foreach over standard variables,...
- Sha Chancellor (5/32) Aug 17 2004 Try this in PERL, C with a for() loop or anything else.. It'll happen
I tried this foreach statement a little while back to neatly format output: #int [char[]] aArray; #aArray["red"] = 0; #aArray["blue"] = 1; #aArray["green] = 2; #foreach(char[] index, int value; aArray) This little bit of code didn't put out a newline after the last element. Why? It reinitialized ctr to 0 for each iteration. (Yes, I printf-ed to find that out.) Is this a bug, or a feature? (I 'fixed' the code by making ctr static.) --Chris Stevenson US Army soldier coding from iraq
Aug 15 2004
In article <cfn4og$aac$1 digitaldaemon.com>, Christopher Stevenson says...#foreach(char[] index, int value; aArray) This little bit of code didn't put out a newline after the last element. Why? It reinitialized ctr to 0 for each iteration. (Yes, I printf-ed to find that out.)I believe this is expected behavior. Every time you "enter" the {} code block, ctr is redeclared and is basically a new variable. Be careful with static, are you sure that ctr will be initialized _at all_ next time you call the function? You could put ctr outside the loop, but I think the best solution in this particular case would be to foreach over aArray.keys instead and use an integer index. If ctr wasn't reinitialized the way it is, this could lead to some very hard to read code in more complicated cases, IMHO. Nick
Aug 15 2004
In article <cfnis6$go4$1 digitaldaemon.com>, Nick says...In article <cfn4og$aac$1 digitaldaemon.com>, Christopher Stevenson says...While I have yet to implement foreach in D.NET, from looking at the compiler code, it appears that D treats a foreach statement as a nested function and therefore has its own local scope. -Deja#foreach(char[] index, int value; aArray) This little bit of code didn't put out a newline after the last element. Why? It reinitialized ctr to 0 for each iteration. (Yes, I printf-ed to find that out.)I believe this is expected behavior. Every time you "enter" the {} code block, ctr is redeclared and is basically a new variable. Be careful with static, are you sure that ctr will be initialized _at all_ next time you call the function? You could put ctr outside the loop, but I think the best solution in this particular case would be to foreach over aArray.keys instead and use an integer index. If ctr wasn't reinitialized the way it is, this could lead to some very hard to read code in more complicated cases, IMHO. Nick
Aug 15 2004
Deja Augustine wrote:In article <cfnis6$go4$1 digitaldaemon.com>, Nick says...I can't speak for how the compiler does foreach over standard variables, that is surely true for classes which overload opApply(). A delegate is passed to opApply; the delegate is basically the code in your foreach block.In article <cfn4og$aac$1 digitaldaemon.com>, Christopher Stevenson says...While I have yet to implement foreach in D.NET, from looking at the compiler code, it appears that D treats a foreach statement as a nested function and therefore has its own local scope.#foreach(char[] index, int value; aArray) This little bit of code didn't put out a newline after the last element. Why? It reinitialized ctr to 0 for each iteration. (Yes, I printf-ed to find that out.)I believe this is expected behavior. Every time you "enter" the {} code block, ctr is redeclared and is basically a new variable. Be careful with static, are you sure that ctr will be initialized _at all_ next time you call the function? You could put ctr outside the loop, but I think the best solution in this particular case would be to foreach over aArray.keys instead and use an integer index. If ctr wasn't reinitialized the way it is, this could lead to some very hard to read code in more complicated cases, IMHO. Nick
Aug 16 2004
In article <cfn4og$aac$1 digitaldaemon.com>, Christopher Stevenson <Christopher_member pathlink.com> wrote:I tried this foreach statement a little while back to neatly format output: #int [char[]] aArray; #aArray["red"] = 0; #aArray["blue"] = 1; #aArray["green] = 2; #foreach(char[] index, int value; aArray) This little bit of code didn't put out a newline after the last element. Why? It reinitialized ctr to 0 for each iteration. (Yes, I printf-ed to find that out.) Is this a bug, or a feature? (I 'fixed' the code by making ctr static.) --Chris Stevenson US Army soldier coding from iraqTry this in PERL, C with a for() loop or anything else.. It'll happen in them too. That's the desired behavior. Move your declaration. You basically make another copy for every iteration of the loop.
Aug 17 2004