I am using Powershell 2.0. When I create a new variable as an array and then set another variable equal to the first, the second variable βmirrorsβ the first. After changing an object in the original array, the same change appears in the second array. For instance,
$array0001=6,7,3,4,0 $array0002=$array0001 $array0001[3]=55 $array0002
with exit
6 7 3 55 0
I notice that when you set the second variable equal to the value of the first variable, with the exception of this time enclosed inside the subexpression operator, modifications to the first array do not affect the second array. For instance,
$array0001=6,7,3,4,0 $array0002=$($array0001) $array0001[3]=55 $array0002
with exit
6 7 3 4 0
Why does including a value in a subexpression statement change the behavior of a variable? Is there any other or better way to prevent the array variables from mirroring each other?
ETA: I found that $array0002=@ ($array0001) and $array0002=&{$array0001} fulfill the same goal.
source share