英文:
How to retrieve all the values from an array which doesn't contain the input list in JMeter
问题
Code for fetching all the value in the response contain the input array,
import groovy.json.JsonSlurper;
def fetchedStudyName = []
1.upto(vars.get('studyNameFetched_matchNr') as int, { index ->
fetchedStudyName.add(vars.get('studyNameFetched_' + index))
});
String tempExcludeStudyName = vars.get('excludeStudyName');
def excludeStudyName = new JsonSlurper().parseText(tempExcludeStudyName);
def valid = fetchedStudyName.findAll { a ->
excludeStudyName.any { a.contains(it) }
}
这段代码用于获取响应中包含在输入数组中的所有值。
英文:
Problem Statement : I have an input array (i.e.excludeStudyName
) and I have a response array, I am trying to fetch the elements which doesn't contain the string given in the input array.
Input Array Value : ["Inflammation","Perf","Test_Study"]
Code for fetching all the value in the response contain the input array,
import groovy.json.JsonSlurper;
def fetchedStudyName = []
1.upto(vars.get('studyNameFetched_matchNr') as int, { index ->
fetchedStudyName.add(vars.get('studyNameFetched_' + index))
});
String tempExcludeStudyName = vars.get('excludeStudyName');
def excludeStudyName = new JsonSlurper().parseText(tempExcludeStudyName);
def valid = fetchedStudyName.findAll { a ->
excludeStudyName.any {a.contains(it)}
}
This is working fine,I am getting 48 values which contain. But I want which doesn't contain this value.
I tried with excludeStudyName.any {!a.contains(it)}
and excludeStudyName.any {!(a.contains(it))}
it is printing all the values from response array.
But not working. how to fix it ?
答案1
得分: 1
如果您想从fetchedStudyName
中删除所有在excludeStudyName
中存在的元素,可以像这样操作:
def toRemove = excludeStudyName.intersect(fetchedStudyName)
fetchedStudyName.removeAll(toRemove)
更多信息:
英文:
If you want to remove from fetchedStudyName
all elements which are present in the excludeStudyName
you could do something like:
def toRemove = excludeStudyName.intersect(fetchedStudyName)
fetchedStudyName.removeAll(toRemove)
More information:
答案2
得分: 1
你的代码几乎正确:
def valid = fetchedStudyName.findAll { a ->
!excludeStudyName.contains(a)
}
应该可以工作。
如果excludeStudyName
是一个很大的列表,你可以考虑将其改为一个集合以加快contains(a)
的速度:
Set excludeStudyName = new JsonSlurper().parseText(tempExcludeStudyName)
def valid = fetchedStudyName.findAll { a ->
!excludeStudyName.contains(a)
}
英文:
You were pretty close:
def valid = fetchedStudyName.findAll { a ->
!excludeStudyName.contains(a)
}
should work.
If excludeStudyName
is a large list you should think about changing it to a set to speed up contains(a)
:
Set excludeStudyName = new JsonSlurper().parseText(tempExcludeStudyName)
def valid = fetchedStudyName.findAll { a ->
!excludeStudyName.contains(a)
}
答案3
得分: 0
Your current code grabs all of the values that match the excludeStudyName and returns them in valid. If your goal is to grab the values that do not match excludeStudyName, then here are some options.
你目前的代码提取与excludeStudyName匹配的所有值,并将它们返回到valid中。如果你的目标是提取与excludeStudyName不匹配的值,那么以下是一些选项。
def included = ["this", "is", "not", "here"]
def excluded = ["is"]
def valid = included.findAll { a ->
!excluded.contains(a)
}
println("valid " + valid)
输出:
valid [this, not, here]
英文:
Your current code grabs all of the values that match the excludeStudyName and returns them in valid. If you're goal is grab the values that do not match excludeStudyName, than here are some options.
def included = ["this", "is", "not", "here"]
def excluded = ["is"]
def valid = included.findAll { a ->
excluded.any {a.contains(it)}
}
def valid1 = included.findAll { a ->
excluded.every { val ->
!a.contains(val)
}
}
def valid2 = included.findAll { a ->
!excluded.contains(a)
}
println("valid " + valid)
println("valid1 " + valid1)
println("valid2 " + valid2)
Output:
valid [this, is]
valid1 [not, here]
valid2 [this, not, here]
答案4
得分: -1
以下是翻译好的部分:
如何看待这种方法,
对你的代码进行轻微更改,
def valid = fetchedStudyName.findAll { a ->
excludeStudyName.every { val ->
!a.contains(val)
}
}
英文:
How about this approach,
a minor change to your code,
def valid = fetchedStudyName.findAll { a ->
excludeStudyName.every { val ->
!a.contains(val)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论