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

前端 未结 6 1640
野的像风
野的像风 2020-12-18 02:17

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:42

    $a = 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,0
    $b = 0..([Math]::ceiling($a.Length / 4) - 1) | 
        % {  @(, $a[($_*4)..($_*4 + 4 - 1)]) } 
    

    Don't know why I had to put a comma after (.

提交回复
热议问题