我尝试在Groovy中从两个带有list的文件创建Map(或关联数组),但结果不是我所期望的。
def componentsFile = "WORKSPACE/Components"
def linksFile = "WORKSPACE/Links"
def components = new File(componentsFile).collect { it.trim() }
def links = new File(linksFile).collect { it.trim() }
def result = [components, links].transpose().collect { component, link ->
[name: component, link: link]
}
println result
我得到的
[[name:component1, component2, component3, component4, link: link1, link2, link3, link4]]
我所期望的
[[name:component1, link:link1], [name:component2, link:link2], [name:component3, link:link3], [name:component4, link:link4]]
1条答案
按热度按时间i7uq4tfw1#
你试图转置包含逗号分隔值的字符串,因此你只在输出中得到2个字段。
你需要提前拆分字符串:
或者对于您的文件(我无法在控制台中复制),var的声明如下所示: