Deep-copy an array

后端 未结 2 849
名媛妹妹
名媛妹妹 2020-12-21 06:15

I\'ve been banging my head against the wall on this one.
I know that if I create an Array in Powershell, then copy the array, it\'ll copy it as a reference type not a va

相关标签:
2条回答
  • 2020-12-21 06:42

    To copy an array, you can do the following:

    $c = (0,0,0)
    $d = $c | foreach { $_ }
    $c[0] = 1
    "c is [$c]"
    "d is [$d]"
    

    Result

    c is [1 0 0]
    d is [0 0 0]
    

    For your particular issue (comparing CPU usage of processes), something more specific would probably be better, as Keith pointed out.

    0 讨论(0)
  • 2020-12-21 06:44

    Technically $d = $c is not any sort of array copy (of reference or value). It's just stashing a reference to the array $c refers to, into $d. I think you only need to grab the array of processes once and then call the Refresh method. You'll have to check the Exited property first to make sure the associated process is still running. Of course, this won't help you if you're interested in any new process that start up. In that case, grab a snapshot of processes at different times, weed out all but the intersection of processes between the two arrays (by process Id) and then compute the differences in their property values - again based on process Id. Take make this easier, you might want to put each snapshot in a hashtable keyed off the process Id.

    0 讨论(0)
提交回复
热议问题