Resize an array

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of turbo mcp turbo mcp 2 years, 1 month ago.

  • Author
    Posts
  • #20381
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Hi there
    I am trying to use a function I found few months ago that will resize an array into pieces so I can do all kind of stuff with it "chunks" like jobs or schedule each chunk
    its probably some small mistake I am doing but I don't see it breaking it into chunks.
    something like this:
    function Resize-Array ([object[]]$InputObject, [int]$SplitSize = 100)
    {
    $length = $InputObject.Length
    for ($Index = 0; $Index -lt $length; $Index += $SplitSize)
    {
    , ($InputObject[$index .. ($index + $splitSize – 1)])
    }
    }
    $numbers=@()
    $numbers= 1..300
    $SplitSize = $numbers.Count/10
    $resizedarray=Resize-Array -InputObject $numbers -SplitSize $splitsize

    any help would be appreciated
    Thanks

  • #20382
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Sorry, too late at the day to post stupid questions:)
    was working fine
    I forgot, I was listing
    $resizedarray
    instead of $resizedarray.count which has exactly the expected "groups"
    or $resizedarray[0] which has the first "group"
    oh well

  • #20383
    Profile photo of turbo mcp
    turbo mcp
    Participant

    small little question about that
    if I do something like this:
    function Resize-Array ([object[]]$InputObject, [int]$SplitSize = 100)
    {
    $length = $InputObject.Length
    for ($Index = 0; $Index -lt $length; $Index += $SplitSize)
    {
    , ($InputObject[$index .. ($index + $splitSize – 1)])
    }
    }

    $numbers= (1..300)
    $SplitSize = $numbers.Count/10
    $resizedarray=Resize-Array -InputObject $numbers -SplitSize $splitsize
    foreach ($array in $resizedarray){
    Write-host "$array" -ForegroundColor Green
    Start-Sleep -Seconds 10
    }

    I get something like this:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60....

    what if I wanted to write-host every item in that sub array like this:
    1
    2
    3
    4
    5
    ....
    how would I access the items in each of these "sub groups"
    Thanks

  • #20387
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Well, why dont you try "Write-Output" instead?

    "Write-Output" returns objects.

    "Write-Host" doesn't return any objects.

    See the help for both of the commands for further explanation.

    /Alexander

  • #20414
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Thanks
    I had two silly questions in one day:)
    all I needed was another foreach and its good to go:)
    but thanks for your reply I learned something new

You must be logged in to reply to this topic.