将字符串数组拆分为最大大小的块

时间:2022-08-25 19:34:30

I'm attempting to ensure an array of unknown length is split in one-or-more arrays with a maximum of 20 elements.

我试图确保将一个未知长度的数组拆分成一个或多个数组,最多包含20个元素。

I've searched around and found several answers for similar questions based on arrays containing number sequences (or maybe I'm just missing something), but nothing for an array of strings.

我已经四处寻找并根据包含数字序列的数组找到了类似问题的几个答案(或者我可能只是遗漏了一些东西),但对于字符串数组却没有。

I've come up with a solution, but it seems a little convoluted and I feel it could probably be better.

我想出了一个解决方案,但它看起来有点令人费解,我觉得它可能会更好。

For example, take this array -

例如,拿这个数组 -

$metricDefinitionsHash= [ordered]@{"this one" = "111"; "that one" = "222"; "another one" = "333"}

And for the sake of this example, that's say I want only a maximum of two elements -

并且为了这个例子,那就是说我只想要最多两个元素 -

$metricsQueryParts = @()
$counter = 0
$metricDefinitionsHash.Keys | ForEach-Object {
    $index = [System.Math]::Floor($counter / 2)
    if ($metricsQueryParts.Length -eq $index)
    {
        $metricsQueryParts += ""
        $metricsQueryParts[$index] = @()
    }
    $metricsQueryParts[$index] += $_
    $counter++
}

So now I have an array of arrays -

所以现在我有一个数组数组 -

@(@("this one", "that one"), @("another one"))

But... Can I achieve my goal in a more efficient way?

但是......我能以更有效的方式实现我的目标吗?

2 个解决方案

#1


2  

Use a for loop, the range operator (..), and the unary array construction operator (,):

使用for循环,范围运算符(..)和一元数组构造运算符(,):

[array]$keys = $metricDefinitionsHash.Keys
$size = 3
$metricsQueryParts = @()
for ($i=0; $i -lt $keys.Count; $i+=$size) {
    $metricsQueryParts += ,$keys[$i..($i+$size-1)]
}

[array]$keys casts the key/value collection .Keys to a generic array (so that the index operator can be used on it).

[array] $ keys将键/值集合.Keys转换为通用数组(以便可以在其上使用索引运算符)。

The expression $i..($i+$size-1) gives you the indexes from $i up to the index before $i+$size, i.e. the next $size elements from the array starting at position $i. If there are less than $size elements the expression will give just the remaining number of elements.

表达式$ i ..($ i + $ size-1)为您提供从$ i到$ i + $ size之前的索引的索引,即从位置$ i开始的数组中的下一个$ size元素。如果元素少于$ size,则表达式将仅提供剩余的元素数。

The unary array construction operator ensures that $keys[...] is appended to $metricsQueryParts as a nested array instead of just concatenating the two arrays.

一元数组构造运算符确保$ keys [...]作为嵌套数组附加到$ metricsQueryParts,而不是仅连接两个数组。

#2


1  

I really like Ansgar's solution but the keys collection isn't an array and as such you can't use the array range syntax. Keys is an OrderedDictionaryKeyValueCollection. I have used out-string -stream to force the OrderedDictionaryKeyValueCollection to an array.

我非常喜欢Ansgar的解决方案,但是密钥集合不是数组,因此您无法使用数组范围语法。 Keys是OrderedDictionaryKeyValueCollection。我使用out-string -stream强制OrderedDictionaryKeyValueCollection到一个数组。

$keys = $metricDefinitionsHash.Keys | out-string -stream
$size = 2
$metricsQueryParts = @()
for ($i=0; $i -lt $keys.Count; $i+=$size) {
    $metricsQueryParts += ,$keys[$i..($i+$size-1)]
}

#1


2  

Use a for loop, the range operator (..), and the unary array construction operator (,):

使用for循环,范围运算符(..)和一元数组构造运算符(,):

[array]$keys = $metricDefinitionsHash.Keys
$size = 3
$metricsQueryParts = @()
for ($i=0; $i -lt $keys.Count; $i+=$size) {
    $metricsQueryParts += ,$keys[$i..($i+$size-1)]
}

[array]$keys casts the key/value collection .Keys to a generic array (so that the index operator can be used on it).

[array] $ keys将键/值集合.Keys转换为通用数组(以便可以在其上使用索引运算符)。

The expression $i..($i+$size-1) gives you the indexes from $i up to the index before $i+$size, i.e. the next $size elements from the array starting at position $i. If there are less than $size elements the expression will give just the remaining number of elements.

表达式$ i ..($ i + $ size-1)为您提供从$ i到$ i + $ size之前的索引的索引,即从位置$ i开始的数组中的下一个$ size元素。如果元素少于$ size,则表达式将仅提供剩余的元素数。

The unary array construction operator ensures that $keys[...] is appended to $metricsQueryParts as a nested array instead of just concatenating the two arrays.

一元数组构造运算符确保$ keys [...]作为嵌套数组附加到$ metricsQueryParts,而不是仅连接两个数组。

#2


1  

I really like Ansgar's solution but the keys collection isn't an array and as such you can't use the array range syntax. Keys is an OrderedDictionaryKeyValueCollection. I have used out-string -stream to force the OrderedDictionaryKeyValueCollection to an array.

我非常喜欢Ansgar的解决方案,但是密钥集合不是数组,因此您无法使用数组范围语法。 Keys是OrderedDictionaryKeyValueCollection。我使用out-string -stream强制OrderedDictionaryKeyValueCollection到一个数组。

$keys = $metricDefinitionsHash.Keys | out-string -stream
$size = 2
$metricsQueryParts = @()
for ($i=0; $i -lt $keys.Count; $i+=$size) {
    $metricsQueryParts += ,$keys[$i..($i+$size-1)]
}