Groovy(或Java)中如何将字符串数组的所有索引包含在一行中
How to include all indexes of a string array into one line in Groovy (Or Java)
如果您在java中有答案,它会足够相似来翻译它。预先感谢您的宝贵时间。
我试图获取一个数组中的所有字符串,并在一个 docker 行中调用它们以创建一个 tar 文件。我对如何创建 tar 文件并不感到困惑。它应该类似于以下内容:
sh "docker save [image1]:tag [image2]:tag etc... > docker.tar"
我的问题是我需要 tar 的图像按以下格式列出:
myMap = [
"StringIndex0": ["keyword": "image0", "unrelatedKeyword": "sentence0"],
"StringIndex1": ["keyword": "image1", "unrelatedKeyword": "sentence1"],
"StringIndex2": ["keyword": "image2", "unrelatedKeyword": "sentence2"],
]
我不能像下面这样执行 for 循环:
for (index in myMap) {
for (img in myMap[index]['keyword']){
sh "docker save ${img}:tag > docker.tar"
}
}
因为这会单独创建 docker save [image1:tag] > docker.tar
和 docker save [image2:tag] > docker.tar
,它们会相互覆盖。我正在寻找能够一次 tar 所有图像的解决方案。
我不确定连接是否有效,因为它可能看起来不正确:
docker save image1 image2 imag3:tag > docker.tar
我希望得到一个适用于数组中任意数量的 StringIndexes 的答案,因此我不会像这样对其进行硬编码:
AllKeywords = ""
for (index in myMap) {
for (img in myMap[index]['keyword']) {
AllKeywords += "${img}"
}
}
sh "docker save AllKeywords[0]:tag AllKeywords[1]:tag > docker.tar"
因为基本上我可以这样做:
sh "docker save myMap[StringIndex0]['keyword']:tag myMap[StringIndex1]['keyword']:tag > docker.tar"
添加后不再考虑 StringIndexes。
我是 SO 的新手,所以如果我的问题根本不清楚,请告诉我,我会尽力解决。
我不熟悉 Groovy 语法,但以下内容应该可以很好地表达这个想法。
all = ""
for (index in myMap) {
for (img in myMap[index]['keyword']) {
all += "${img}:tag "
}
}
sh "docker save " + all + "> docker.tar"
您的示例输出还有方括号 ([]
)、[image1:tag] [image2:tag] ...
,因此您可能需要添加这些。
假设您的地图实际上是地图的地图,这里有一个选项:
String saveTargets = map.values().collect { "${it['keyword']}:tag" }.join(' ')
sh "docker save $saveTargets > docker.tar"
如果您的 tag
实际上是参数化的,只需将其替换为 $tag
。
(因为这是 Groovy,有很多选择,但这应该可以帮助您。)
如果您在java中有答案,它会足够相似来翻译它。预先感谢您的宝贵时间。
我试图获取一个数组中的所有字符串,并在一个 docker 行中调用它们以创建一个 tar 文件。我对如何创建 tar 文件并不感到困惑。它应该类似于以下内容:
sh "docker save [image1]:tag [image2]:tag etc... > docker.tar"
我的问题是我需要 tar 的图像按以下格式列出:
myMap = [
"StringIndex0": ["keyword": "image0", "unrelatedKeyword": "sentence0"],
"StringIndex1": ["keyword": "image1", "unrelatedKeyword": "sentence1"],
"StringIndex2": ["keyword": "image2", "unrelatedKeyword": "sentence2"],
]
我不能像下面这样执行 for 循环:
for (index in myMap) {
for (img in myMap[index]['keyword']){
sh "docker save ${img}:tag > docker.tar"
}
}
因为这会单独创建 docker save [image1:tag] > docker.tar
和 docker save [image2:tag] > docker.tar
,它们会相互覆盖。我正在寻找能够一次 tar 所有图像的解决方案。
我不确定连接是否有效,因为它可能看起来不正确:
docker save image1 image2 imag3:tag > docker.tar
我希望得到一个适用于数组中任意数量的 StringIndexes 的答案,因此我不会像这样对其进行硬编码:
AllKeywords = ""
for (index in myMap) {
for (img in myMap[index]['keyword']) {
AllKeywords += "${img}"
}
}
sh "docker save AllKeywords[0]:tag AllKeywords[1]:tag > docker.tar"
因为基本上我可以这样做:
sh "docker save myMap[StringIndex0]['keyword']:tag myMap[StringIndex1]['keyword']:tag > docker.tar"
添加后不再考虑 StringIndexes。
我是 SO 的新手,所以如果我的问题根本不清楚,请告诉我,我会尽力解决。
我不熟悉 Groovy 语法,但以下内容应该可以很好地表达这个想法。
all = ""
for (index in myMap) {
for (img in myMap[index]['keyword']) {
all += "${img}:tag "
}
}
sh "docker save " + all + "> docker.tar"
您的示例输出还有方括号 ([]
)、[image1:tag] [image2:tag] ...
,因此您可能需要添加这些。
假设您的地图实际上是地图的地图,这里有一个选项:
String saveTargets = map.values().collect { "${it['keyword']}:tag" }.join(' ')
sh "docker save $saveTargets > docker.tar"
如果您的 tag
实际上是参数化的,只需将其替换为 $tag
。
(因为这是 Groovy,有很多选择,但这应该可以帮助您。)