英文:
Powershell - array - Concatenate two arrays separated with comma
问题
以下是翻译好的部分:
我有什么?
$array1 = "string01", "string02", "string03"
$array2 = "another01", "antoher02", "anoter03"
我希望什么?
输出文件:
- 示例01:
string01,another01
string02,another02
string03,another03
如何将两个数组逐行连接在一起?这是因为我需要将$array01的值01与$array02的值01组合在一起,并用逗号分隔它们。
我尝试使用foreach,但它会将$array01的每个值与$array02的每个值组合在一起,例如:
- 示例02:
string01,another01
string01,another02
string01,another03
string02,another01
string02,another02
string02,another03
string03,another01
string03,another02
string03,another03
但我需要像示例01那样的输出。
我正在使用PowerShell!谢谢
我尝试使用foreach,但没有成功实现这一点!
英文:
What I have?
$array1 = "string01", "string02", "string03"
$array2 = "another01", "antoher02", "anoter03"
What I wish?
output-file:
- Example01:
string01,another01
string02,another02
string03,another03
How can I cocatenate two arrays in one and do that line by line? It's because I need to combine value 01 from $array01 with value01 from $array02, separating them with comma.
I have tried to use foreach, but this one combine every value from $array01 with every value from $array02, for example:
- Example02:
string01,another01
string01,another02
string01,another03
string02,another01
string02,another02
string02,another03
string03,another01
string03,another02
string03,another03
But I need the output likes the Example01.
I am using powershell! Thanks
I have tried to use foreach, but this one combine every value from $array01 with every value from $array02, but no success doing that!
答案1
得分: 1
你可以使用一个 For
循环来简单地完成这个任务。类似于:
For($i=0; $i -lt $array1.count; $i++){
$array1[$i],$array2[$i] -join ','
}
英文:
You would use a For
loop to do this simply. Something like:
For($i=0; $i -lt $array1.count; $i++){
$array1[$i],$array2[$i] -join ','
}
答案2
得分: 1
Enumerable.Zip
正好实现了你所寻找的功能:
$array1 = 'string01', 'string02', 'string03'
$array2 = 'another01', 'antoher02', 'anoter03'
[System.Linq.Enumerable]::Zip(
[string[]] $array1,
[string[]] $array2,
[Func[string, string, string]] {param($x, $y) "$x,$y" })
# 输出:
# string01,another01
# string02,antoher02
# string03,anoter03
英文:
Enumerable.Zip
does exactly what you're looking for:
$array1 = 'string01', 'string02', 'string03'
$array2 = 'another01', 'antoher02', 'anoter03'
[System.Linq.Enumerable]::Zip(
[string[]] $array1,
[string[]] $array2,
[Func[string, string, string]] {param($x, $y) "$x,$y" })
# Outputs:
# string01,another01
# string02,antoher02
# string03,anoter03
答案3
得分: 0
我使用了一个ForEach循环来完成这个任务。可能有很多方法可以实现这个目标。
ForEach($x in $array1){
$index = $array1.IndexOf($x)
$combined += @($array1[$index] + "," + $array2[$index])
}
在上面的代码块中,循环中的当前项是$x,我们从$array1中获取$x的索引,因为这是我们要循环的数组。然后,我们只需使用该索引在另一个数组中获取对应的值,并通过连接运算符"+" 将它们拼接在一起。
$combined的输出:
string01,another01
string02,antoher02
string03,anoter03
英文:
I did this with a ForEach loop. There are probably a million ways to skin this cat.
ForEach($x in $array1){
$index = $array1.IndexOf($x)
$combined += @($array1[$index] + "," + $array2[$index])
}
In the above block the current item in the loop is going to be $x, we get the index of $x from $array1 because that's the array we are looping. We just use that index on the other array, and we glue it all together by concatenating with "+" symbols.
Output of $combined:
string01,another01
string02,antoher02
string03,anoter03
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论