如何从一个数组中检索所有不包含输入列表的值在 JMeter 中

huangapple go评论46阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月8日 21:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定