J.Doe
J.Doe

Reputation: 37

Replace Array Value with Array Value

I want to replace a template of data $Arraytest with actual data from an XML file Arraytest2.

So I to replace the $Arraytest.Values with those from $Arraytest2.Values and save them for further process.

$Arraytest = @{
    TLC     = 'TLC'
    Crew3LC = 'Crew3LC'
    MyText  = 'MyText'
}

$Arraytest2 = @{
    TLC     = 'FWE'
    Crew3LC = 'KMU'
    MyText  = 'Hello'
}

foreach ($Value in $Arraytest) {
    $Value.Values
}

Upvotes: 0

Views: 64

Answers (2)

committhisgit
committhisgit

Reputation: 110

Your objects are hashtables, not arrays:

$Arraytest | Get-Member

TypeName: System.Collections.Hashtable     

So you can update using the built in hashtable keys:

$Arraytest = @{
    TLC      = 'TLC'
    Crew3LC  = 'Crew3LC'
    MyText   = 'MyText'
}

$Arraytest2 = @{ 
    TLC      = 'FWE'
    Crew3LC  = 'KMU'
    MyText   = 'Hello'
}

foreach($key in $($Arraytest.keys)){
    $ArrayTest[$key] = $ArrayTest2[$key] 
}

$ArrayTest

Name                           Value                                                                                                                                                                                 
----                           -----                                                                                                                                                                                 
Crew3LC                        KMU                                                                                                                                                                                   
TLC                            FWE                                                                                                                                                                                   
MyText                         Hello   

Upvotes: 1

henrycarteruk
henrycarteruk

Reputation: 13227

You can use Clone() for a shallow copy:

$Arraytest = $Arraytest2.Clone()

A shallow copy will be fine for this case, but see Remarks for further info on this.

Upvotes: 0

Related Questions