I want to create data sets from variable length multidimensional arrays, but I'm not sure how best to go about it.
I've thought of calculating the permutation of the array and then looping 开发者_开发问答through the values to create the new data set, but that approach seems fragile.
Below are example inputs and outputs.
Input
[ [id: "Denver", value: ['$', '%']], [id: "datestamp", value: [201106]], [id: "price", value: [1, 2]] ]
Expected Output
[ ["Denver$2011061"], ["Denver%2011061"], ["Denver$2011062"], ["Denver%2011062"] ]
Assuming in Groovy, you have:
def data = [ [id: "Denver", value: ['$', '%']], [id: "datestamp", value: [201106]], [id: "price", value: [1, 2]] ]
Then you can do:
def output = data*.value.combinations()*.join('').collect { [ "${data[0].id}$it" ] }
And output will have the value:
[[Denver$2011061], [Denver%2011061], [Denver$2011062], [Denver%2011062]]
精彩评论