8

Need guidance on the syntax of nested looping in groovy. How to use iterator to print values of (value of a.name, value of b.name) here?

List a
a.each {
    print(it.name)
    List b = something
    b.each {
        print(value of a.name, value of b.name)
    }
}
cfrick
  • 35,203
  • 6
  • 56
  • 68
user2093576
  • 3,082
  • 7
  • 32
  • 43

1 Answers1

23
List a
a.each { x ->
    println(x.name)
    List b = something
    b.each { y ->
        println(x.name + y.name)
    }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • If you need to stop processing when encountering some condition, or skip the `a` or `b` for some condition, then you'd need to use the `for`-`in` loop because the `break` and `continue` keywords don't work --or more accurately, work in an unexpected manner-- with `each` method enclosures. – Vorg van Geir Jan 31 '15 at 06:59
  • how about return? I have a return statement inside nested each loops and the return statement seems to have been ignored – jeremy mordkoff Feb 04 '22 at 20:11
  • https://stackoverflow.com/a/20461947/6509 – tim_yates Feb 04 '22 at 23:25