Check if a list's value contains in a string

groovy

Solution

Do you have an example that fails with some example xml? I ask because this (which you say at the top of your question crashes) actually works:

def vrs=["6.0","6.1","6.1.0"]
def test=[ version:'6.1 HotFix11' ]

vrs.each { ver-> println test.version.contains( ver ) }

and prints:

false
true
false

But I cannot find a problem with your other code, as I don't know what `'E://Projects/agent6.1.xml'` contains...

Problem

I have a list `def vrs=["6.0","6.1","6.1.0"]` (versions) and I get a map in a function with this value `def test=[version:6.1 HotFix11]`. How can I check whether `test`'s `version` value matches with the list `vrs`? I tried this `vrs.each{ver-> println test.version.contains(ver)}` but it gives `Exception in thread "main" java.lang.StackOverflowError` Update Turns out, there was something wrong with my code. I tried the test case in a small Groovy script and it works. Here is the full code: ``` private Map params private def root private def nineBelow XmlHandler(String xml) { nineBelow=["6.0","6.1","6.1.0"] params=[:] root=new XmlParser().parseText(xml) } def getParams() { if(root.product.version.size()>0) { params.version=root.product.version.text() } /*nineBelow.each { println params.version //even this throws java.lang.StackOverflowError //println "$it , ${params.version}" //println ver.getClass()+", "+params.version.getClass() }*/ println nineBelow.each{ver-> println params.version.contains(ver)} /*I need to check whether `params.version` matches with `nineBelow` list, so i'll check for condition here*/ params } ``` Another class which calls `getParams()` ``` static main(args) { String fileContents = new File('E://Projects/agent6.1.xml').text XmlHandler xm=new XmlHandler(fileContents) def params=xm.getParams() println params } ``` Update Even `println nineBelow.each { println params.version}` gives me `Exception in thread "main" java.lang.StackOverflowError` More Update It worked only after the below code ``` def ver=params.version println nineBelow.each { println ver.contains(it) } ``` What is the problem here?

Original source

Related problems