logo       

Re: standard for loop: msg#00196

lang.groovy.user

Subject: Re: standard for loop

err... that s/b:

foo = ['a', 'b', 'c']
for (i in 0..foo.size()) {println foo[i]}
a
b
c

... but you get the idea. :-)

On Nov 29, 2004, at 11:39 PM, Ken Pelletier wrote:

Scott,

You can just use a range, such as 1...3

for (i in 1..3) { println i }
1
2
3

You can use a suitable variable in a range, too, of course, so:

foo = ['a', 'b', 'c']
for (i in 0...foo.size()) { println i }
a
b
c

If you use the '..' operator for a range, the high value of the range is included, whereas the '...' operator does not include it.

So, '..' with the upper bound being the collection length works like the usual for loop "< length" idiom.

- Ken

On Nov 29, 2004, at 11:17 PM, Scott Stirling wrote:

Hi,

Is there a Groovy idiom (with a counter) to use instead of a standard 'for' loop? I need to use the counter from the loop iterations.

I get this exception when I code a standard 'for' loop (i.e., for (int i =0; i < foo.size(); i++):

Exception in thread "main" NOT YET IMPLEMENTED: standard for loop
at org.codehaus.groovy.syntax.parser.ASTBuilder.forStatement(ASTBuilder.j ava:1026)
at org.codehaus.groovy.syntax.parser.ASTBuilder.statement(ASTBuilder.java :773)
at org.codehaus.groovy.syntax.parser.ASTBuilder.topLevelStatement(ASTBuil der.java:327)
at org.codehaus.groovy.syntax.parser.ASTBuilder.build(ASTBuilder.java: 198)
at org.codehaus.groovy.control.SourceUnit.convert(SourceUnit.java:339)

Thank you,
Scott Stirling
Framingham, MA




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise