micmac
micmac

Reputation: 11

Replace elements in an array by element from another array on PowerShell

I need to replace some elements in an array with elements from another array but I dont know the structure on powershell.

For instance I have:

$A = @("t","o","p")
$B = @("u","g","j")

I want t to become u, o to become g.

I guess you need to use a Foreach and create a loop but I'm not sure at all about the syntax.

By the way I'm working on XML data how do you save the changes on the active folder?

Upvotes: 1

Views: 2360

Answers (1)

Sid
Sid

Reputation: 2676

If you are looking to replace all the elements in $A to the corresponding ones in $B, why not just do $A = $B.

If there is some sort of condition, use something like this.

for ($i = 0; $i -lt $($B.Count); $i++)
{
    if ("Insert Conditon here")
    {
        $A[$i] = $B[$i]
    }
}
$A

All of this happens in the memory. And this is not XML format. Since you mentioned some folder, use the out-File cmdlet to save to disk. I dont know what else to tell you. Not enough information.

Upvotes: 3

Related Questions