Private/PSArray.ps1
function New-ArrayList { [CmdletBinding()] param( ) [System.Collections.ArrayList] $List = New-Object System.Collections.ArrayList <# Mathias Rørbo Jessen: The pipeline will attempt to unravel the list on assignment, so you'll have to either wrap the empty arraylist in an array, like above, or call WriteObject explicitly and tell it not to, like so: $PSCmdlet.WriteObject($List,$false) #> return , $List } function Add-ToArray { [CmdletBinding()] param( [System.Collections.ArrayList] $List, [ValidateNotNullOrEmpty()][Object[]] $Element ) foreach ($E in $Element) { Write-Verbose $E $List.Add($E) > $null } } function Remove-FromArray { param( [System.Collections.ArrayList] $List, [Object] $Element, [switch] $LastElement ) if ($LastElement) { $LastID = $List.Count - 1 $List.RemoveAt($LastID) > $null } else { $List.Remove($Element) > $null } } function Split-Array { <# .SYNOPSIS Split an array .NOTES Version : July 2, 2017 - implemented suggestions from ShadowSHarmon for performance .PARAMETER inArray A one dimensional array you want to split .EXAMPLE Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -parts 3 .EXAMPLE Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -size 3 #> param($inArray, [int]$parts, [int]$size) if ($parts) { $PartSize = [Math]::Ceiling($inArray.count / $parts) } if ($size) { $PartSize = $size $parts = [Math]::Ceiling($inArray.count / $size) } $outArray = New-Object 'System.Collections.Generic.List[psobject]' for ($i = 1; $i -le $parts; $i++) { $start = (($i - 1) * $PartSize) $end = (($i) * $PartSize) - 1 if ($end -ge $inArray.count) {$end = $inArray.count - 1} $outArray.Add(@($inArray[$start..$end])) } return , $outArray } |