powershell array clear方法和赋值
powershell array clear method and assignment
赋值运算符 =“将变量的值设置为指定值”,如 reference doc 中所述。毫不奇怪,先前已分配给另一个变量的变量(在我的例子中是一个数组)的更改不会影响后一个变量的值。
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:\>
但是当使用方法 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:\> write-host "A after clearing V: $A"
A after clearing V:
PS C:\>
似乎应用于 $V 的 clear() 方法也适用于 $A。好像是通过引用赋值,够奇怪的,只针对这个方法。事实上,如果在清除$V 后为其分配一个新值,$A 仍然只受到清除的影响。
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:\>
有可能避免这种影响,尽管不完全相同:$A=$V.clone() 或使用 cmdlet Clear-Variable -name V 或 $V=$null 而不是 $V.clear() 或其他比有人建议的更好的方法。
但我的问题是:
如何解释 clear 方法对 $V 对另一个数组 $A 的影响 "propagation"?
已在 PS ver.5.1.
上完成测试
对我来说你看到的很正常,我试着解释一下自己:
$V="a", "b", "c"
$a = $V
现在 $a
包含与 $V
相同的引用。如果您清除 $V
,则 $a
将被清除。
现在如果你写 $V = "b","c"
,你会影响 $V
与新选项卡的引用。并且此引用与您对 $a
的影响不同。所以现在如果你清除 $V
,$a
不会被清除。
我够清楚了吗?
使用 Clone()
方法获取数组的 true 副本。
$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
有关解释,请阅读 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]
99
Although the contents of $array2
were changed in this example, this
affects $array1
as well, because they are both identical. The
variables $array1
and $array2
internally 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]
1
Whenever 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
顺便说一句,你可以满足术语值类型、引用类型和指针类型更多时候……
赋值运算符 =“将变量的值设置为指定值”,如 reference doc 中所述。毫不奇怪,先前已分配给另一个变量的变量(在我的例子中是一个数组)的更改不会影响后一个变量的值。
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:\>
但是当使用方法 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:\> write-host "A after clearing V: $A"
A after clearing V:
PS C:\>
似乎应用于 $V 的 clear() 方法也适用于 $A。好像是通过引用赋值,够奇怪的,只针对这个方法。事实上,如果在清除$V 后为其分配一个新值,$A 仍然只受到清除的影响。
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:\>
有可能避免这种影响,尽管不完全相同:$A=$V.clone() 或使用 cmdlet Clear-Variable -name V 或 $V=$null 而不是 $V.clear() 或其他比有人建议的更好的方法。
但我的问题是:
如何解释 clear 方法对 $V 对另一个数组 $A 的影响 "propagation"?
已在 PS ver.5.1.
上完成测试对我来说你看到的很正常,我试着解释一下自己:
$V="a", "b", "c"
$a = $V
现在 $a
包含与 $V
相同的引用。如果您清除 $V
,则 $a
将被清除。
现在如果你写 $V = "b","c"
,你会影响 $V
与新选项卡的引用。并且此引用与您对 $a
的影响不同。所以现在如果你清除 $V
,$a
不会被清除。
我够清楚了吗?
使用 Clone()
方法获取数组的 true 副本。
$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
有关解释,请阅读 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] 99
Although the contents of
$array2
were changed in this example, this affects$array1
as well, because they are both identical. The variables$array1
and$array2
internally 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] 1
Whenever 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
顺便说一句,你可以满足术语值类型、引用类型和指针类型更多时候……