joy
joy

Reputation: 216

Compare two lists in jenkins pipeline

In my pipeline, I have two lists and want to compare it and print output accordingly

1- println abc
[aaa, bbb, ccc]

2- println xyz
[bbb, ccc]

I need to print the output to a file like:

aaa not present in xyz
bbb present 
ccc preset

Code I tried:

def test []
test = abc - xyz
println test

Upvotes: 0

Views: 863

Answers (2)

daggett
daggett

Reputation: 28564

def abc = ['aaa', 'bbb', 'ccc']
def xyz = ['bbb', 'ccc']

//simple 
println 'present in xyz: ' + abc.intersect(xyz).join(', ')
println 'not present in xyz: ' + abc.minus(xyz).join(', ')

//with for-each
for(i in abc){
   if(i in xyz) println "present in xyz: $i"
   else println "not present in xyz: $i"
}

Upvotes: 3

Oplop98
Oplop98

Reputation: 230

You can try something along those lines:

abc.each{valueOne ->
   doesValueExist = false
   xyz.each{ valueTwo->
      if(valueOne.equals(valueTwo}{
      doesValueExist = true
      } 
    }
   if(doesValueExist){
     echo "${valueOne} is present"
   } else { 
      echo  "${valueOne} is not present"
   }
}

Upvotes: 0

Related Questions