计算数组中数据的出现

编程入门 行业动态 更新时间:2024-10-21 12:44:03
本文介绍了计算数组中数据的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个像这样的数组:

I have an array like this:

OptiPlex 790 Precision WorkStation T7500 Precision WorkStation T7400 Precision T1500 Precision WorkStation T3500 CELSIUS R650 VMware Virtual Platform Precision T1500 OptiPlex GX620

我想获取数组的计数并将其添加到新数组中.

I want to get the count of array and will add that in new array.

Element:Count OptiPlex 790 1 Precision WorkStation T7500 1 Precision WorkStation T7500 1

我想将此值存储在新数组中.因此,我将在以后/其他地方使用它.

I want to store this value in new array. So, i will use it later/somewhere else.

$array = @() for ($i=1; $i -le $rowMax-1; $i++) { $Model = $sheet.Cells.Item($rowModel+$i, $colModel).text Write-Host ("I am reading data on row: " + $i) $array += $Model } $array | Group

目前,上述脚本可以正常工作,但是我不知道如何将这些数据添加到新数组中.

At the moment above script is working fine but I don't know how I can add this data to new array.

推荐答案

我将构造一个哈希表而不是数组:

I would construct a hash table rather than an array:

$Models = @( 'OptiPlex 790' 'Precision WorkStation T7500' 'Precision WorkStation T7400' 'Precision T1500' 'Precision WorkStation T3500' 'CELSIUS R650' 'VMware Virtual Platform' 'Precision T1500' 'OptiPlex GX620' ) # Create a hashtable $ModelCount = @{} # Iterate over each entry in the array, increment value count in hashtable $Models |ForEach-Object { $ModelCount[$_]++ }

现在,您的哈希表可以计算出您需要的所有信息:

Now your hash table countains all the information you need:

PS C:\> $ModelCount Name Value ---- ----- OptiPlex 790 1 VMware Virtual Platform 1 Precision WorkStation T7500 1 Precision T1500 2 CELSIUS R650 1 Precision WorkStation T7400 1 OptiPlex GX620 1 Precision WorkStation T3500 1

您可以轻松添加新值:

# Let's say you found another 3 OptiPlex GX620 somewhere: $ModelCount['OptiPlex GX620'] += 3

和条目:

$ModelCount['New Model']++

您仍然可以对其进行迭代:

And you can still iterate over it:

PS C:\> $ModelCount.Keys |Sort-Object |ForEach-Object { >>> Write-Host "We have $($ModelCount[$_]) of $_ in stock" >>> } We have 1 of CELSIUS R650 in stock We have 1 of OptiPlex 790 in stock We have 4 of OptiPlex GX620 in stock We have 2 of Precision T1500 in stock We have 1 of Precision WorkStation T3500 in stock We have 1 of Precision WorkStation T7400 in stock We have 1 of Precision WorkStation T7500 in stock We have 1 of VMware Virtual Platform in stock

更多推荐

计算数组中数据的出现

本文发布于:2023-11-22 13:50:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!