本文介绍了PowerShell的数组赋值分配变量,而不是价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有创建一个数组的程序的一个例子,然后尝试该阵列多次的值分配到另一个数组作为多维数组

I have an example of a program that creates an array, and then attempts to assign the value of that array multiple times into another array as a multidimensional array.

$a =@(0,0,0)
$b = @($a,$a,$a)
$b[1][2]=2
$b
'And $a is changed too:'
$a

输出是:

PS E:\Workarea> .\what.ps1
0
0
2
0
0
2
0
0
2
And $a is changed too:
0
0
2

因此​​,在这种情况下,变量实际上指向原始变量。这是非常意外的行为。这是相当不错的,一个能做到这一点,但我从来没有使用工会那么多在我的C编程。但我想办法实际上只是不变量的值的分配。

So in this instance, the variable is actually pointing to the original variable. This is very unexpected behavior. It's rather neat that one can do this, although I never did use unions that much in my C programming. But I'd like a way to actually just do the assignment of the value, not of the variable.

$b = @($a.clone(),$a.clone(),$a.clone())

我想会的工作,但东西告诉我,有可能是东西多一点优雅的比。

I guess would work, but something tells me that there may be something a little more elegant than that.

感谢您的输入。

这是PowerShell 2.0中的Windows 7下的64位。

This is PowerShell 2.0 under Windows 7 64-bit.

推荐答案

要分配 $ A 的值 $ B 而不是参照 $ A ,你可以用在 $变量()。任何在 $()获取命令使用它之前评估,所以 $($ A)等同于 0,0,0

To assign the values of $a to $b instead of the reference to $a, you can wrap the variable in $(). Anything in $() gets evaluated before using it in the command, so $($a) is equivalent to 0, 0, 0.

$a =@(0,0,0)
$b = @($($a),$($a),$($a))
$b[1][2]=2
$b
'$a is not changed.'
$a

这篇关于PowerShell的数组赋值分配变量,而不是价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:03