Powershell break a long array into a array of array with length of N in one line?

前端 未结 6 2019
梦毁少年i
梦毁少年i 2020-12-18 02:24

For example, given a list 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8 and a number 4, it returns a list of list with length of 4, that is (1, 2, 3, 4), (5, 6, 7, 8),

6条回答
  •  情书的邮戳
    2020-12-18 02:37

    Clear-Host
    
    $s = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
    
    $count = $s.Length
    
    $split = $count/2
    
    $split --
    
    $b = $s[0..$split]
    
    $split ++
    
    $a = $s[$split..$count]
    
    write-host "first array"
    
    $b
    
    write-host "next array"
    
    $a
    
    #clean up
    Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
    

提交回复
热议问题