How can I “zip” two arrays in PowerShell?

后端 未结 2 838
太阳男子
太阳男子 2021-02-05 13:47

I want to zip two arrays, like how Ruby does it, but in PowerShell. Here\'s a hypothetical operator (I know we\'re probably talking about some kind of goofy pipeline, but I just

2条回答
  •  独厮守ぢ
    2021-02-05 14:09

    # Multiple arrays
    $Sizes = @("small", "large", "tiny")
    $Colors = @("red", "green", "blue")
    $Shapes = @("circle", "square", "oval")
    
    # The first line "zips"
    $Sizes | ForEach-Object { $i = 0 }{ @{size=$_; color=$Colors[$i]; shape=$Shapes[$i]}; $i++ } `
     | ForEach-Object { $_.size + " " + $_.color + " " + $_.shape }
    

    Outputs:

    small red circle
    large green square
    tiny blue oval
    

提交回复
热议问题