How to add same keys with different values to a hashtable in powershell?

淺唱寂寞╮ 提交于 2020-12-14 23:51:58

问题


Here is the code where i used array of hashtables

$podnumbers = @(1,3,1)

$podInfo = $null
$buffer = 0
$podarray = foreach ($pd in $podnumbers) {
    $podinfo = @()
    for($i=0;$i -lt $pd;$i = $i+1) {
    $pod = Read-Host -Prompt "Assign the pod numbers for",$esxarray[$buffer]
    Write-Output `n
    }
    @{$pd = $pod}
$buffer = $buffer + 1
}

Inputs I gave for $pod is 1 = 1 ; 3 = 2,4,6 ; 1 = 3

I want my arrays of hashtable to be like below,

Key : 1
Value : 1
Name : 1

Key : 3
Value : 2,4,6
Name : 3

Key : 1
Value : 3
Name : 1

But the actual output I got is,

Key : 1
Value : 1
Name : 1

Key : 3
Value : 2
Name : 3

Key : 3
Value : 4
Name : 3

Key : 3
Value : 6
Name : 3

Key : 1
Value : 3
Name : 1

回答1:


If you want to add multiple values to the same key, you need to test for whether the key is already present and act accordingly:

$keys = 1,2,3,1
$value = 'a value'
$hashtable = @{}

foreach($key in $keys){
  if(-not $hashtable.ContainsKey($key)){
    # First time we see this key, let's make sure the value is a resizable array
    $hashtable[$key] = @()
  }

  # Now we can safely add values regardless of whether the key already exists
  $hashtable[$key] += $value
}



回答2:


To complement the answer from @Matthias:
You can't have duplicate keys in a hash table. This is by design. Hash tables contain a list of unique keys based on a binary search algorithm where each key is linked to a specific object (value).

There are two workarounds:

1. add an array of values to a specific hash table key:

$Keys = 1,2,2,3
$Values = 'a'..'d'
$HashTable = @{}
$i = 0
foreach($key in $Keys){ [array]$HashTable[$Key] += $Values[$i++] }

Iterating
The disadvantage is that you lose the order of the items (regardless if you use an ordered dictionary simply because some values share a single key).

Foreach ($Key in $Hashtable.Keys) {
    Foreach ($Value in @($Hashtable[$Key])) {
        Write-Host $Key '=' $Value
    }
}

3 = d
2 = b
2 = c
1 = a

Searching
The advantage is that the items are binary indexed and therefor quickly found.

$SearchKey = 2
Foreach ($Value in @($Hashtable[$SearchKey])) {
    Write-Host $SearchKey '=' $Value
}

2 = b
2 = c

2. or you might create a list (array) of hash tables (Key Value Pairs)

$Keys = 1,2,2,3
$Values = 'a'..'d'
$i = 0
$KeyValuePairs = foreach($key in $keys){ @{ $Key = $Values[$i++] } }

Iterating
The advantage is that the items will stay in order.
(Note that you have to invoke the GetEnumerator() method twice)

$KeyValuePairs.GetEnumerator().GetEnumerator() | Foreach-Object {
    Write-Host $_.Key '=' $_.Value
}

1 = a
2 = b
2 = c
3 = d

Searching
The disadvantage is that searching for the items is slower and little more comprehensive.

$SearchKey = 2
$KeyValuePairs.GetEnumerator().GetEnumerator() | 
    Where-Object Key -eq $SearchKey | Foreach-Object {
    Write-Host $_.Key '=' $_.Value
}

2 = b
2 = c



回答3:


I am doing this in a separate answer as your new redefined objective more specific than the initial question appeared. Instead, each response should be assigned to a the concerned podnumber.

Script:

$podnumbers = @(1,3,1)
$Inputs = 1,2,4,6,3
$c = 0
$podarray = foreach ($pd in $podnumbers) {
    $podinfo = @{}
    for($i=0; $i -lt $pd; $i = $i+1) {
        # $pod = Read-Host -Prompt "Assign the pod numbers for",$esxarray[$buffer]
        [array]$podinfo[$pd] += $Inputs[$c++]
    }
    $podinfo
}
$podarray

Output:

Name                           Value
----                           -----
1                              {1}
3                              {2, 4, 6}
1                              {3}


来源:https://stackoverflow.com/questions/64591142/how-to-add-same-keys-with-different-values-to-a-hashtable-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!