PowerShell の連想配列のコピー [PowerShell]
PowerShell の配列変数や連想配列変数のコピーは実体への参照がコピーされるだけなのを忘れないようにしよう。
簡単な確認。次のように連想配列を作って値を入れてからコピーして、
http://msdn.microsoft.com/en-us/library/system.collections.hashtable.clone(VS.85).aspx
を見ると .NET Framework のハッシュテーブルの Clone メソッドもシャロウコピーと明記されているので、代入はこのメソッドが呼ばれているのかな?
ディープコピーしたい場合にはどうすればいいのだろう。
GetEnumerator メソッドを使って再帰的に全部コピーするように書くのがいいのでしょうか。一段だけなら次のように割と単純に書けますが、
簡単な確認。次のように連想配列を作って値を入れてからコピーして、
$hashtable1 = @{}コピーしたほうに別の値を代入すると、
$hashtable2 = @{}
$hashtable1["key1"] = 101
$hashtable1["key2"] = 102
$hashtable2 = $hashtable1
$hashtable2["key1"] = 201元のhashtabele1の値もかわります。
$hashtable2["key3"] = 203
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
}
タグ:powershell 連想配列
沒有醫生的處方
cialis 20 mg cost http://cialisyoues.com/ Generic for cialis
by Cialis alternative (2018-04-14 09:32)
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)