Skip to content

Instantly share code, notes, and snippets.

@drandarov-io
Created June 28, 2024 13:48
Show Gist options
  • Select an option

  • Save drandarov-io/5198a4d05d9e3e209f1880b7d6fec855 to your computer and use it in GitHub Desktop.

Select an option

Save drandarov-io/5198a4d05d9e3e209f1880b7d6fec855 to your computer and use it in GitHub Desktop.

Revisions

  1. drandarov-io created this gist Jun 28, 2024.
    31 changes: 31 additions & 0 deletions Select-Zip.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    param (
    [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
    [object[]] $Arrays,

    [switch] $Split,

    [ValidateSet("Longest", "Shortest")]
    [string] $OutputMode = "Shortest"
    )

    begin {
    $zipped = [System.Linq.Enumerable]::Zip($Arrays[0], $Arrays[1], [Func[Object, Object, Object[]]]{ @($args[0], $args[1]) })

    # $longest = $Arrays | Sort-Object Length -Descending | Select-Object -First 1
    # $null * ($Arrays.Length - 1) + $longest | ForEach-Object {

    for ($i = 2; $i -lt $Arrays.Length; $i++) {
    $zipped = [System.Linq.Enumerable]::Zip($zipped, $Arrays[$i], [Func[Object, Object, Object[]]]{ $args[0] + @($args[1]) })
    }
    }

    process {
    $zipped | ForEach-Object {
    $obj = @{}
    for ($j = 0; $j -lt $_.Length; $j++) {
    $obj["Value_$j"] = $_[$j]
    }

    $Split ? [pscustomobject]$obj : , $_
    }
    }