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

后端 未结 10 1604
清歌不尽
清歌不尽 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 03:02

    Use clone:

    foreach($key in ($myHash.clone()).keys){
        $myHash[$key] = 5
    }
    

    Or in the one-liner:

    $myHash = ($myHash.clone()).keys | % {} {$myHash[$_] = 5} {$myHash}
    
    0 讨论(0)
  • 2020-12-29 03:05

    There is a much simpler way of achieving this. You cannot change the value of a hashtable while enumerating it because of the fact that it's a reference type variable. It's exactly the same story in .NET.

    Use the following syntax to get around it. We are converting the keys collection into a basic array using the @() notation. We make a copy of the keys collection, and reference that array instead which means we can now edit the hashtable.

    $myHash = @{}
    $myHash["a"] = 1
    $myHash["b"] = 2
    $myHash["c"] = 3
    
    foreach($key in @($myHash.keys)){
        $myHash[$key] = 5
    }
    
    0 讨论(0)
  • 2020-12-29 03:05

    You do not need to clone the whole hashtable for this example. Just enumerating the key collection by forcing it to an array @(...) is enough:

    foreach($key in @($myHash.keys)) {...
    
    0 讨论(0)
  • 2020-12-29 03:06

    I'm new to PowerShell, but I'm quite a fan of using in-built functions, because I find it more readable. This is how I would tackle the problem, using GetEnumerator and Clone. This approach also allows one to reference to the existing hash values ($_.value) for modifying purposes.

    $myHash = @{}
    $myHash["a"] = 1 
    $myHash["b"] = 2
    $myHash["c"] = 3
    
    $myHash.Clone().GetEnumerator() | foreach-object {$myHash.Set_Item($_.key, 5)}
    
    0 讨论(0)
提交回复
热议问题