Splitting an Array in smaller arrays

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5273

    by the_hutch at 2012-11-13 13:27:34

    I have a utility that determines the number of pingable IPs in each of our network ranges and puts them in a text file. I've used these IPs to create several arrays.


    $Range1 = Get-Content .\Range1.txt
    $Range2 = Get-Content .\Range2.txt
    $Range3 = Get-Content .\Range3.txt
    $Range4 = Get-Content .\Range4.txt

    Each range usually contains over 20,000 pingable IPs. So running a script against each individual range takes some serious time. I'd like to divide each array into several smaller arrays to run several background jobs in parallel. But I'm not sure the best way to go about doing this. Any thoughts? Thanks in advance.

    by nohandle at 2012-11-14 07:15:51

    Hi, this is my approach to tackle the problem.

    the array is split to smaller arrays and output
    if you capture the output you will get array of arrays

    powershell does return only 10 values if 1..100 is specified on an array of length 10 and no errors. so I don`t have to split the array precisely

    $example = 1..11

    function Split-Array ([object[]]$InputObject,[int]$SplitSize=100)
    {
    $length=$InputObject.Length
    for ($Index = 0; $Index -lt $length; $Index += $SplitSize)
    {
    #, encapsulates result in array
    #-1 because we index the array from 0
    ,($InputObject[$index..($index+$splitSize-1)])
    }
    }
    $result= Split-Array -InputObject $example -SplitSize 10

You must be logged in to reply to this topic.