The assignment operator = “Sets the value of a variable to the specified value” as said in the reference doc . Not surprisingly, changes on a variable (in my case an array) that has been previously assigned to another variable do not affect the value of this latter one.
PS C:\> $V="a", "b", "c"
PS C:\> $A=$V
PS C:\> write-host "A before new value of V: $A"
A before new value of V: a b c
PS C:\> $V="e","f"
PS C:\> write-host "A after new value of V: $A"
A after new value of V: a b c
PS C:\>
But when the method clear() is used the behaviour seems different.
PS C:\> $V="a", "b", "c"
PS C:\> $A=$V
PS C:\> write-host "A before clearing V: $A"
A before clearing V: a b c
PS C:\> $V.clear()
PS C:\> write-host "A after clearing V: $A"
A after clearing V:
PS C:\>
It seems that clear() method applied on $V acts also on $A. As if the assignment were by reference, strange enough, only for this method. In fact, if a new value is assigned to $V after having cleared it, $A is still affected only by the clear.
PS C:\> $V="a", "b", "c"
PS C:\> $A=$V
PS C:\> write-host "A before clearing V: $A"
A before clearing V: a b c
PS C:\> $V.clear()
PS C:\> $V="e","f"
PS C:\> write-host "A after clearing V: $A"
A after clearing V:
PS C:\>
There are possibilities to avoid this effect, although not precisely identical: $A=$V.clone() or use of cmdlet Clear-Variable -name V or $V=$null instead $V.clear() or perhaps others better than these that somebody could suggest.
But my question is:
how to explain the "propagation" of the effect of clear method on $V to the other array $A?
Tests have been done on PS ver.5.1.
Use Clone() method to get true copy of an array.
$V = "a", "b", "c"
$A = $V.Clone()
Write-Host "A before new value of V: $A"
$V = "e","f"
Write-Host "A after new value of V: $A"
A before new value of V: a b c A after new value of V: a b c
For explanation, read Copying Arrays and Hash Tables:
Copying arrays or hash tables from one variable to another works, but may produce unexpected results. The reason is that arrays and hash tables are not stored directly in variables, which always store only a single value. When you work with arrays and hash tables, you are dealing with a reference to the array or hash table. So, if you copy the contents of a variable to another, only the reference will be copied, not the array or the hash table. That could result in the following unexpected behavior:
$array1 = 1,2,3 $array2 = $array1 $array2[0] = 99 $array1[0] 99Although the contents of
$array2were changed in this example, this affects$array1as well, because they are both identical. The variables$array1and$array2internally reference the same storage area. Therefore, you have to create a copy if you want to copy arrays or hash tables,:$array1 = 1,2,3 $array2 = $array1.Clone() $array2[0] = 99 $array1[0] 1Whenever you add new elements to an array (or a hash table) or remove existing ones, a copy action takes place automatically in the background and its results are stored in a new array or hash table. The following example clearly shows the consequences:
# Create array and store pointer to array in $array2: $array1 = 1,2,3 $array2 = $array1 # Assign a new element to $array2. A new array is created in the process and stored in $array2: $array2 += 4 $array2[0]=99 # $array1 continues to point to the old array: $array1[0] 1
BTW, you can meet the terms Value type, Reference type and Pointer type more often…
For me what you see is normal, I try to explain myself :
$V="a", "b", "c"
$a = $V
now $a contains the same reference as $V. if you clear $V then $a is cleared.
now if you write $V = "b","c", you affect $V with the reference of a new tab. and this reference is not the same that you affect to $a. so now if you clear $V, $a is not cleared.
Am I clear enought ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With