groovy - is there any implicit variable to get access to item index in "each" method

groovy

Solution

you can use `eachWithIndex()`

"one two three four".split().eachWithIndex() { entry, index ->
      println "${index} : ${entry}" }

this will result in

0 : one
1 : two
2 : three
3 : four

Problem

Is there any way to remove variable "i" in the following example and still get access to index of item that being printed ? ``` def i = 0; "one two three".split().each { println ("item [ ${i++} ] = ${it}"); } ``` =============== EDIT ================ I found that one possible solution is to use "eachWithIndex" method: ``` "one two three".split().eachWithIndex {it, i println ("item [ ${i} ] = ${it}"); } ``` Please do let me know if there are other solutions.

Original source