Powershell – array – 用逗号分隔连接两个数组

huangapple go评论76阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月28日 00:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定