Merging hashtables in PowerShell: how?

前端 未结 12 1920
猫巷女王i
猫巷女王i 2021-01-07 18:26

I am trying to merge two hashtables, overwriting key-value pairs in the first if the same key exists in the second.

To do this I wrote this function which first remo

12条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 19:17

    I just wanted to expand or simplify on jon Z's answer. There just seems to be too many lines and missed opportunities to use Where-Object. Here is my simplified version:

    Function merge_hashtables($htold, $htnew) {
        $htold.Keys | ? { $htnew.ContainsKey($_) } | % {
            $htold.Remove($_)
        }
        $htold += $htnew
        return $htold
    }
    

提交回复
热议问题