Updating hash table values in a 'foreach' loop in PowerShell

后端 未结 10 1646
清歌不尽
清歌不尽 2020-12-29 02:03

I\'m trying to loop through a hash table and set the value of each key to 5 and PowerShell gives an error:

$myHash = @{}
$myHash[\"a\"] = 1
$myHash[\"b\"] =          


        
10条回答
  •  执笔经年
    2020-12-29 02:57

    $myHash = @{
        Americas = 0;
        Asia = 0;
        Europe = 0;
    }
    
    $countries = @("Americas", "Asia", "Europe", "Americas", "Asia")
    
    foreach($key in $($myHash.Keys))
    {
        foreach($Country in $countries)
        {
            if($key -eq $Country)
            {
                $myHash[$key] += 1
            }
        }
    }
    

    Updating a hash value if array elements matched with a hash key.

提交回复
热议问题