Jacob
Jacob

Reputation: 4021

Double loop in Groovy

How can I double loop using each if I have a structure like this:

Termin 1
[  [ 1][2 ][3 ]   ]
Termin 2
[  [1 ] [2 ] [3]  ]

Termin.each(){
            println("first");
           it.each(){
               println("second"); // 1 2 3 
           }
        }

Upvotes: 0

Views: 2585

Answers (1)

user800014
user800014

Reputation:

it is used when you don't define the attribute name. You can just change the name:

def nested = [[1],[2],[3]]

nested.each { n ->
  n.each { s ->
    print "Nested: $s \n"
  }
}

UPDATE
it is implicit to the wrapped closure, so if you are fluent with Groovy semantics, you can also use

def nested = [[1],[2],[3]]

nested.each { 
  // `it` is meant for the nested.each{}
  it.each {
    // `it` is meant for the it.each{}
    print "Nested: $it \n"
  }
}

Both of the approach yield the same result.

Upvotes: 3

Related Questions