1

我想将此列表转置为 groovy 脚本。有两个列表

result_name = ["API(示例)","管理门户(示例)","Component1","Component2",]

result_ids = ["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]

我期待类似 list[0][0], list[1][1].... 的输出。例如:

API (example) 3wrhs4vp3sp5
Management Portal g2828br1gzw9
Component1 68pnwhltxcq0
Component2 fy8g2nvvdg15

我正在尝试使用

def 结果 = [[result_name], [result_ids]].transpose()

但结果是:

结果:[[["API (example)","Management Portal (example)","Component1","Component2",], ["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]]]

编辑:更新了问题中的示例代码:

proc1 = ['/bin/bash', '-c', "curl https://api.statuspage.io/v1/pages/lbh0g6b5mwnf/components?api_key=<key>"].execute()
proc2 = ['/bin/bash', '-c', "grep -Po '\"name\": *\\K\"[^\"]*\"'| tr '\n' ', '"].execute()
proc3 = ['/bin/bash', '-c', "curl https://api.statuspage.io/v1/pages/lbh0g6b5mwnf/components?api_key=<key>"].execute()
proc4 = ['/bin/bash', '-c', "grep -Po '\"id\": *\\K\"[^\"]*\"'| tr '\n' ', '"].execute()

all_name = proc1 | proc2
all_ids = proc3 | proc4
def result_name = [all_name.text]
def result_ids = [all_ids.text]

println result_name
println result_ids

def result = [result_name, result_ids].transpose()

结果

["API (example)","Management Portal (example)","Component1","Component2",]
["3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]
Result: [["API (example)","Management Portal (example)","Component1","Component2",, "3wrhs4vp3sp5","g2828br1gzw9","68pnwhltxcq0","fy8g2nvvdg15",]]
 
4

2 回答 2

0

在您展示的代码示例中,您将result_nameresult_ids列表都包装到了附加列表中。重写以下代码:

def result = [[result_name], [result_ids]].transpose()

到:

def result = [result_name, result_ids].transpose()

你会得到预期的结果。

于 2021-08-23T08:16:08.007 回答
0

回答我自己的问题。

经过几次调试后,我意识到我的列表只有一个字符串,因此 split() 并没有期待那个奇怪的输入。newstr我可以通过输入下面的 split(',') 代码来打印第一个字符串:

all_name = proc1 | proc2
all_ids = proc3 | proc4
    
result_name = [all_name.text]
result_ids = [all_ids.text]

newstr = result_name[0]
result_newstr = newstr.split(',')

newids = result_ids[0]
result_newids = newids.split(',')

def aa = []
for(int i = 0;i< result_newstr.size(); i++) {
    a = result_newstr[i].concat(" ").concat(result_newids[i])
  aa.add(a)
}
return aa

结果:

"API (example)" "3wrhs4vp3sp5"
"Management Portal (example)" "g2828br1gzw9"
"Component1" "68pnwhltxcq0"
"Component2" "fy8g2nvvdg15"
于 2021-08-24T12:57:22.553 回答