0

Ok I'm sure I'm doing something wrong here.

result = []
for (aMp in arMap)  {
    println("0 " + result)
    println("1 " + aMp)
    println("2 " + delegate.findSingleMap(aMp))
    result.addAll(delegate.findSingleMap(aMp))
    println "3 " + result
}
return result

The println result are the following: (I have 2 element in arMap, so it print the four value 2 times)

0 []
1 [ID:XXX, Type:4]
2 [[First:21/Nov/2013,  Type:4, error code:SXR07, ID:XXX, Test ID:5]]
3 [[First:21/Nov/2013,  Type:4, error code:SXR07, ID:XXX, Test ID:5]]
0 [[First:21/Nov/2013,  Type:4, error code:SXR07, ID:XXX, Test ID:5]]
1 [ID:YYY, Type:4]
2 [[First:12/Oct/2012,  Type:4, error code:SXR07, ID:YYY, Test ID:6]]
3 [[First:12/Oct/2012,  Type:4, error code:SXR07, ID:YYY, Test ID:6]]

As you can see the findSingleMap function work properly, but the second time I use the AddAll, my result array loose the value XXX.

What am I doing wrong?

5
  • You post not all you code. Where string that starts from 0 come from? Commented Sep 19, 2014 at 10:44
  • @talex just added. I was wondering if the result reset before the beginning of next cycle and I added a print Commented Sep 19, 2014 at 10:44
  • 1
    does your findSingleMap also work with a variable result? Using a simplified version of your code works fine for me (groovy 2.2.1) and addAll works as advertised. Commented Sep 19, 2014 at 11:20
  • @cfrick You got it right. Want to turn it into an answer? My findSingleMap was result = delegate for (aEl in map) { result = result.findAll({ it[aEl.key] == aEl.value }) } return result Commented Sep 19, 2014 at 11:26
  • @cfrick I'm still quite young in groovy scripting and I'm loosing sight of scope of variable all the time!!! Commented Sep 19, 2014 at 11:29

1 Answer 1

1

As stated by the OP int the comments the method findSingleMap modifies the (global) result variable.

for (aEl in map) { 
    result = result.findAll { it[aEl.key] == aEl.value } 
}

return result

Not writing def in front a variable declares it (in simple scripts) global, which might result in strange behaviour like this. So don't do it, unless you have to codegolf.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.