Home > database >  Compare two lists in jenkins pipeline
Compare two lists in jenkins pipeline

Time:12-28

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

CodePudding user response:

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"
}

CodePudding user response:

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"
   }
}
  • Related