SSブログ
QLOOKアクセス解析

PowerShell の連想配列のコピー [PowerShell]

PowerShell の配列変数や連想配列変数のコピーは実体への参照がコピーされるだけなのを忘れないようにしよう。
簡単な確認。次のように連想配列を作って値を入れてからコピーして、
$hashtable1 = @{}
$hashtable2 = @{}

$hashtable1["key1"] = 101
$hashtable1["key2"] = 102

$hashtable2 = $hashtable1
コピーしたほうに別の値を代入すると、
$hashtable2["key1"] = 201
$hashtable2["key3"] = 203
元のhashtabele1の値もかわります。
http://msdn.microsoft.com/en-us/library/system.collections.hashtable.clone(VS.85).aspx
を見ると .NET Framework のハッシュテーブルの Clone メソッドもシャロウコピーと明記されているので、代入はこのメソッドが呼ばれているのかな?
ディープコピーしたい場合にはどうすればいいのだろう。
GetEnumerator メソッドを使って再帰的に全部コピーするように書くのがいいのでしょうか。一段だけなら次のように割と単純に書けますが、
foreach ($item in $hashtable1.GetEnumerator())
{
     $hashtable3[$item.Key] = $item.Value
}
連想配列の要素がさらに連想配列なら再帰的にする必要あるのでちょっと面倒?。


nice!(0)  コメント(2)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 2

Cialis alternative

沒有醫生的處方
cialis 20 mg cost http://cialisyoues.com/ Generic for cialis
by Cialis alternative (2018-04-14 09:32) 

ThomasSleen

Samsung Smart Switch <a href=http://galaxysmartswitch.com/article/samsung-smart-switch>galaxysmartswitch</a> is sole of the best solutions to persuade and reject all your mighty files and settings from rhyme active phone to another.
Download Samsung smart whip instead of youк device, upgrade scheme to latest version Samsung well-groomed flog 2019 Trounce all-inclusive guides how to upgrade
Samsung smart redirect on you insigne, you can download all manifestation Samsung smart switch. Bolster to android and ios devices
by ThomasSleen (2019-06-05 14:05) 

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

PowerShell で Winform..ISE か PowerGUI か ブログトップ
人気ブログランキングへ
 

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。