user2916886
user2916886

Reputation: 845

how to filter elements of list of list based on the value of a column in Groovy

I am working with Groovy and have this list of list with certain number of elements:

def rList = [["Using Patient Feedback","Completed","No Results Available","Condition1","Other","Phase I","18 Years and older   (Adult, Senior)","Other","https://folder1//folder2"],["The Effect of a Education ","Completed","No Results Available","Condition1","Behavioral","Glycemic Control","18 Years and older   (Adult, Senior)","Other","https://folder1//folder3"],["The Public Private Partnership","Completed","No Results Available","Condition1","Enhanced Education","Improvement in A1C","18 Years to 85 Years   (Adult, Senior)","NIH","https://folder1//folder4"],["Evaluation of a Pilot","Completed","No Results Available","Condition1","Behavioral","Change in percent of total calories","10 Years to 19 Years   (Child, Adult)","Other","https://folder1//folder5"],["Self Management Skills","Completed","No Results Available","Condition1","Behavioral","Metabolic control","18 Years and older   (Adult, Senior)","Industry","https://folder1//folder6"]]

Each list withing the above list of list has 9 elements/columns. I also have a search string defined as below:

def fundingType = "NIH"

Now what I want to do is look for the presence of this string in the 8th element/column of each list and if its present then store rest 8 elements in 8 different variables - var1,var2,....var8. How can I do this in Groovy?

Upvotes: 0

Views: 178

Answers (2)

Rao
Rao

Reputation: 21369

You should be able to get it easily using find as shown below:

def fundingType = "NIH"
println rList.find{ fundingType == it[7] }

You can quickly try it online demo

Instead of 8 variables, you could change it as:

def result = rList.find{ fundingType == it[7] }

And use result[0], result[1]...without using additional 8 variables.

Upvotes: 2

Steinar
Steinar

Reputation: 5950

Something like:

def rList = [["Using Patient Feedback","Completed","No Results Available","Condition1","Other","Phase I","18 Years and older   (Adult, Senior)","Other","https://folder1//folder2"],["The Effect of a Education ","Completed","No Results Available","Condition1","Behavioral","Glycemic Control","18 Years and older   (Adult, Senior)","Other","https://folder1//folder3"],["The Public Private Partnership","Completed","No Results Available","Condition1","Enhanced Education","Improvement in A1C","18 Years to 85 Years   (Adult, Senior)","NIH","https://folder1//folder4"],["Evaluation of a Pilot","Completed","No Results Available","Condition1","Behavioral","Change in percent of total calories","10 Years to 19 Years   (Child, Adult)","Other","https://folder1//folder5"],["Self Management Skills","Completed","No Results Available","Condition1","Behavioral","Metabolic control","18 Years and older   (Adult, Senior)","Industry","https://folder1//folder6"]]
def fundingType = "NIH"
def fundingColumnIndex = 7

def fundingRow = rList.find { it[fundingColumnIndex] == fundingType }
fundingRow.remove(fundingColumnIndex)
assert fundingRow.size() == 8
def (v1, v2, v3, v4, v5, v6, v7, v8) = fundingRow
[v1, v2, v3, v4, v5, v6, v7, v8].each { println(it) }

Output:

The Public Private Partnership
Completed
No Results Available
Condition1
Enhanced Education
Improvement in A1C
18 Years to 85 Years   (Adult, Senior)
https://folder1//folder4

Upvotes: 2

Related Questions