1

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?

3
  • I am afraid you are gonna get some downvotes. Commented Jul 3, 2018 at 12:46
  • Welcome to Stack Overflow! Hi, thanks, ... are considered noise; Commented Jul 3, 2018 at 12:49
  • Welcome as a new user, you should take the tour and read How to Ask. Commented Jul 3, 2018 at 14:20

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.