简体   繁体   English

在PowerShell上的另一个数组中按元素替换数组中的元素

[英]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. 我需要用另一个数组中的元素替换数组中的一些元素,但我不知道powershell上的结构。

For instance I have: 比如我有:

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

I want t to become u , o to become g . 我想t成为uo成为g

I guess you need to use a Foreach and create a loop but I'm not sure at all about the syntax. 我想你需要使用Foreach并创建一个循环,但我完全不确定语法。

By the way I'm working on XML data how do you save the changes on the active folder? 顺便说一下,我正在处理XML数据,如何保存活动文件夹上的更改?

If you are looking to replace all the elements in $A to the corresponding ones in $B , why not just do $A = $B . 如果您正在寻找替换所有的元素$A在相应的一些$B ,为什么不只是做$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. 这不是XML格式。 Since you mentioned some folder, use the out-File cmdlet to save to disk. 由于您提到了某个文件夹,请使用out-File cmdlet保存到磁盘。 I dont know what else to tell you. 我不知道还能告诉你什么。 Not enough information. 信息不足。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM