linux数组切片,在Powershell中切片数组(或列表)的更好方法

Bacon Bits' helpful answer shows how to simplify your code with the help of .., the range operator, but it would be nice to have a general-purpose chunking (partitioning, batching) mechanism; however, as of PowerShell 7.0, there is no built-in feature.

This GitHub feature suggestion proposes adding a -ReadCount parameter to Select-Object, analogous to the parameter of the same name already defined for Get-Content.

If you'd like to see this feature implemented, show your support for the linked issue there.

使用该功能后,您可以执行以下操作:

$i = 0

Get-ADUser -Filter * -Properties Mail |

Select-Object -ReadCount 30 | # WISHFUL THINKING: output 30-element arrays

ForEach-Object {

$_ | Export-Csv -Path ($PSScriptRoot + "\ASSFAM" + ("{0:d2}" -f ++$i) + ".csv") -Delimiter ";" -Encoding UTF8 -NoTypeInformation

}

In the interim, you could use custom function Select-Chunk (source code below): replace Select-Object -ReadCount 30 with Select-Chunk -ReadCount 30 in the snippet above.

这是它如何工作的简单演示:

PS> 1..7 | Select-Chunk -ReadCount 3 | ForEach-Object { "$_" }

1 2 3

4 5 6

7

The above shows that the ForEach-Object script block receive the following

three arrays, via $_, in sequence:

1, 2, 3, 4, 5, 6, and , 7

(When you stringify an array, by default you get a space-separated list of its elements; e.g., "$(1, 2, 3)" yields 1 2 3).

Select-Chunk source code:

The implementation uses a [System.Collections.Generic.Queue[object]] instance to collect the inputs in batches of fixed size.

function Select-Chunk {

.SYNOPSIS

Chunks pipeline input.

.DESCRIPTION

Chunks (partitions) pipeline input into arrays of a given size.

By design, each such array is output as a *single* object to the pipeline,

so that the next command in the pipeline can process it as a whole.

That is, for the next command in the pipeline $_ contains an *array* of

(at most) as many elements as specified via -ReadCount.

.PARAMETER InputObject

The pipeline input objects binds to this parameter one by one.

Do not use it directly.

.PARAMETER ReadCount

The desired size of the chunks, i.e., how many input objects to collect

in an array before sending that array to the pipeline.

0 effectively means: collect *all* inputs and output a single array overall.

.EXAMPLE

1..7 | Select-Chunk 3 | ForEach-Object { "$_" }

1 2 3

4 5 6

7

The above shows that the ForEach-Object script block receive the following

three arrays: (1, 2, 3), (4, 5, 6), and (, 7)

#>

[CmdletBinding(PositionalBinding = $false)]

[OutputType([object[]])]

param (

[Parameter(ValueFromPipeline)]

$InputObject

,

[Parameter(Mandatory, Position = 0)]

[ValidateRange(0, [int]::MaxValue)]

[int] $ReadCount

)

begin {

$q = [System.Collections.Generic.Queue[object]]::new($ReadCount)

}

process {

$q.Enqueue($InputObject)

if ($q.Count -eq $ReadCount) {

, $q.ToArray()

$q.Clear()

}

}

end {

if ($q.Count) {

, $q.ToArray()

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值