2016-03-31 14 views
0

Ich habe ein Array wie folgt:Count Vorkommen von Daten in Array

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

ich die Anzahl der Array erhalten möchten und dass in neuen Array hinzuzufügen.

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

Ich möchte diesen Wert in einem neuen Array speichern. Also, ich werde es später/woanders verwenden.

Im obigen Skript funktioniert gut, aber ich weiß nicht, wie ich diese Daten zu neuen Array hinzufügen kann.

Antwort

1

ich eher eine Hash-Tabelle würde konstruieren als ein 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[$_]++ } 

Jetzt enthält Ihre Hash-Tabelle alle Informationen, die Sie benötigen:

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 

Und Sie können auf einfache Weise neue Werte hinzufügen:

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

und Einträge:

$ModelCount['New Model']++ 

Und Sie können immer noch über sie iterieren:

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 
+0

Danke für Ihre Antwort. Ich werde das nachprüfen und dich informieren. – Ironic

+0

Eine Frage. Ich kann $ ($ ModelCount [$ _]) diesen Rückgabe-Modell-Namen und $ _ Return Count sehen. Kann ich diesen Wert einer Variablen zuweisen? z. B. $ count = $ _ und $ name = $ ($ ModelCount [$ _]) Bitte beraten. – Ironic

+0

Sicher, natürlich kannst du :) –

2

Es gibt ein Group-Object Cmdlets, die Sie dafür verwenden können:

$array | group | select Name, Count 

Ausgang:

Name      Count 
----      ----- 
OptiPlex 790     1 
Precision WorkStation T7500  1 
Precision WorkStation T7400  1 
Precision T1500     2 
Precision WorkStation T3500  1 
CELSIUS R650     1 
VMware Virtual Platform   1 
+0

hoThanks für Ihre Antwort. Ich werde versuchen, es dich wissen zu lassen. abgestimmt. Wie kann ich diese Daten zu einem Array hinzufügen? Irgendwelche Ratschläge. – Ironic

+1

$ result = $ array | Gruppe | Wählen Sie Name, Count – Martin

+0

@Martin Vielen Dank für Ihren Kommentar. $ arr = $ array | Gruppe | Select Count gibt keinen Wert zurück. Aber wenn ich $ arr = $ array | Gruppe | Wählen Sie Name, zählen, dann funktioniert es gut. Irgendein Kommentar dazu? – Ironic

Verwandte Themen