Searching for the contents of an Array in another array
I'm using groovy in SOAPUI and am currently trying establish whether the response from 2 endpoints yields the same results.
I'm converting my XML response into an array for each endpoint and then looping through Array1 and then Array2 to confirm the开发者_StackOverflow社区 presence of each item in Array1.
This is very ugly code and I did look at maps, but the syntax confused me. I wsn't sure how I'd create such a map of an array. How do you create the keys in such an instance?
In the meantime, for the sake of my wellbeing..Is there a more elegant way of achieving the following:
def outerCount = 0
def innerCount = 0
for( i in arrayOfInvoicesSTAGE)
{
for( j in arrayOfInvoicesSTAGE2)
{
if (i == j)
{
log.info outerCount+" FOUND "+arrayOfInvoicesSTAGE[outerCount] + " in both responses"
log.info "STAGE: "+i+" STAGE2: "+j
}
}
outerCount++
}
Also! for bonus thumbs up! Can anyone advise me on how I mightadd a break to the inner loop so that it jumps out once a match has been found? I've seen JS code that allows this, but couln't get something similar to work in groovy
Indeed, the above is the smelliest of code...but I've got to grease the joints somehow..it's been a long time! :)
As ever, any tips are appreciated.
Lists have an intersect method in Groovy
def a = [ 1, 2, 3, 4, 5 ]
def b = [ 2, 3, 6 ]
a.intersect( b ).each { item ->
log.info "Found $item in both responses"
}
I think that does what you want?
def a = ["1thing","2thing","3thing"]
def b = ["1thing","2thing","3thing","4thing"]
// should return any element not in both lists
if (a.size < b.size ) {
b - a
} else {
a - b
}
精彩评论