Groovy迭代列表变量

时间:2016-05-31 13:35:22

标签: groovy

我正在编写一个脚本来收集字段名称(dialogPartyASelection_ *&& dialogPartyBSelection_ *),然后比较这两个值以查看它们是否匹配。在比较之前,字段的完整列表(选择)被分解为“组”。我可以突破某些部分(IE,通过迭代比较两个字段值)并成功测试,但是当将所有内容整合在一起时,脚本似乎无法正确比较。我可能正在接近这个/设置自己做错了,(已经开始玩创建地图,以A方为键,以B方为价值)。

代码片段:

    // Test Variables
PartyBSelection_Propertieshamster = 'Accepted'
PartyBSelection_Propertieszembra = 'Agreed'
PartyBSelection_Propertiesdogs = 'Agreed'
PartyBSelection_Propertiescats  = 'Decision taken'
PartyASelection_Propertieshamster = 'Accepted'
PartyASelection_Propertieszembra = 'Agreed'
PartyASelection_Propertiesdogs = 'Agreed'
PartyASelection_Propertiescats = 'Decision taken'
// example of selections(there are lots of entries for A/B party) = ['dialogPartyBSelection_Communication','dialogPartyASelection_Housing','dialogPartyASelection_Income','PartyASelection_Properties']
def selectedGroup = { s -> selections.findAll { it.contains s}} // for pulling groups of questions from list
def isAgreed = { a, b -> (a in ['Agreed', 'Decision taken','Accepted'] && b in ['Agreed', 'Decision taken','Accepted']) } // for comparing values

for(questions in selectedGroup("Properties")){
   {k -> percentCompleteProperties += isAgreed("PartyASelection_${k}", "PartyBSelection_${k}")? 1 : 0}
   println questions
   println percentCompleteProperties
}

当前输出:

PartyBSelection_Propertiescats

0

PartyBSelection_Propertieshamster

0

PartyBSelection_Propertiesdogs

0

PartyBSelection_Propertieshamster

0

PartyASelection_Propertiescats

0

PartyASelection_Propertieshamster

0

PartyASelection_Propertiesdogs

0

PartyASelection_Propertieshamster

0

1 个答案:

答案 0 :(得分:0)

这是示例代码 但我改变了测试数据等 请检查此逻辑是否适合您的情况。

// Test Variables
def testVariables = [
    'PartyBSelection_PropertiesAssets':'Accepted',
    'PartyBSelection_PropertiesDebts': 'Agreed',
    'PartyBSelection_PropertiesMoney': 'Agreed',
    'PartyBSelection_PropertiesSpecialGoods':'Decision taken',
    'PartyASelection_PropertiesAssets':'Accepted',
    'PartyASelection_PropertiesDebts':'Agreed',
    'PartyASelection_PropertiesMoney':'Agreed',
    'PartyASelection_PropertiesSpecialGoods':'Decision taken'
]

List<String> selections= [
    'PropertiesAssets',
    'PropertiesDebts',
    'PropertiesMoney',
    'PropertiesSpecialGoods',
    'PropertiesAssets',
    'PropertiesDebts',
    'PropertiesMoney',
    'PropertiesSpecialGoods'
]


def selectedGroup = { s -> selections.findAll { it.contains s}}
List<String> okLabels = ['Agreed', 'Decision taken','Accepted']
def isAgreed = {a, b ->
    (testVariables[a] in okLabels && testVariables[b] in okLabels)
}

List<Map<String, Integer>> result = selectedGroup("Properties").collect {String question ->
    [(question) : isAgreed("PartyASelection_${question}", "PartyBSelection_${question}")? 1 : 0 ]
}

assert result == [
    ['PropertiesAssets':1],
    ['PropertiesDebts':1],
    ['PropertiesMoney':1],
    ['PropertiesSpecialGoods':1],
    ['PropertiesAssets':1],
    ['PropertiesDebts':1],
    ['PropertiesMoney':1],
    ['PropertiesSpecialGoods':1]
]

其他方式

def r = [:]
def questionsCount = selectedGroup("Properties").size()
selectedGroup("Properties").eachWithIndex {String question, Integer i ->
    println "question:${question}(${i+1}/${questionsCount})"
    r.put(question,isAgreed("PartyASelection_${question}", "PartyBSelection_${question}")? 1 : 0 )
}
// This version includes all records in to the one map.
// Also, a record that is duplicated (as key) is overwritten.
r == [PropertiesAssets:1, PropertiesDebts:1, PropertiesMoney:1, PropertiesSpecialGoods:1]

然后输出:

question:PropertiesAssets(1/8)
question:PropertiesDebts(2/8)
question:PropertiesMoney(3/8)
question:PropertiesSpecialGoods(4/8)
question:PropertiesAssets(5/8)
question:PropertiesDebts(6/8)
question:PropertiesMoney(7/8)
question:PropertiesSpecialGoods(8/8)