将PowerShell数组切成小数组 [英] Slice a PowerShell array into groups of smaller arrays

查看:137
本文介绍了将PowerShell数组切成小数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据一个变量将单个数组转换为一组较小的数组。因此, 0,1,2,3,4,5,6,7,8,9 将变为 0,1,2 3,4,5 6,7,8 9 当大小为3时。

I would like to convert a single array into a group of smaller arrays, based on a variable. So, 0,1,2,3,4,5,6,7,8,9 would become 0,1,2,3,4,5,6,7,8,9 when the size is 3.

我当前的做法:

$ids=@(0,1,2,3,4,5,6,7,8,9)
$size=3

0..[math]::Round($ids.count/$size) | % { 

    # slice first elements
    $x = $ids[0..($size-1)]

    # redefine array w/ remaining values
    $ids = $ids[$size..$ids.Length]

    # return elements (as an array, which isn't happening)
    $x

} | % { "IDS: $($_ -Join ",")" }

产生:

IDS: 0
IDS: 1
IDS: 2
IDS: 3
IDS: 4
IDS: 5
IDS: 6
IDS: 7
IDS: 8
IDS: 9

我希望它是

IDS: 0,1,2
IDS: 3,4,5
IDS: 6,7,8
IDS: 9

我想念什么?

推荐答案

您可以使用,$ x 而不只是 $ x

<$ c文档中$ c> about_Operators 部分具有以下内容:

, Comma operator                                                  
   As a binary operator, the comma creates an array. As a unary
   operator, the comma creates an array with one member. Place the
   comma before the member.

这篇关于将PowerShell数组切成小数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆