Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays PowerShell替换数组中的值_Arrays_Powershell_Arrayofarrays - Fatal编程技术网

Arrays PowerShell替换数组中的值

Arrays PowerShell替换数组中的值,arrays,powershell,arrayofarrays,Arrays,Powershell,Arrayofarrays,我是PowerShell的新手,我需要一些关于如何替换数组中的值的支持。请看一下我的示例: [array[]]$nodes = @() [array[]]$nodes = get-NcNode | select-object -property Node, @{Label = "slot"; expression = {@("a")*4}} $nodes Node slot ---- ---- nn01 {a,a,a,

我是PowerShell的新手,我需要一些关于如何替换数组中的值的支持。请看一下我的示例:

[array[]]$nodes = @()
[array[]]$nodes = get-NcNode | select-object -property Node, @{Label = "slot"; expression = {@("a")*4}}

$nodes
Node       slot
----       ----
nn01       {a,a,a,a}
nn02       {a,a,a,a}
nn03       {a,a,a,a}
nn04       {a,a,a,a}             
 
$nodes[0].slot[0]
      a

$nodes[0].slot[0] = "b"            #I try to replace a with b
$nodes[0].slot[0]
      a                            #It didn’t work

$nodes[0].slot.SetValue("b",0)     #I try to replace a with b
$nodes[0].slot[0]
      a                            #It didn’t work

$nodes[0] | Add-Member -MemberType NoteProperty -Name slot[0] -Value "b" -Force
$nodes[0]
Node       slot      slot[0]
----       ----      -------
nn01       {a,a,a,a} b              #That’s not what I wanted
如果您确实需要一组数组(键入
[array[]]
),则问题的解决方法如下:

$nodes[0][0].slot[0] = "b" 
也就是说,每个
$nodes
元素本身就是一个数组,按照填充
$nodes
的方式,每个
[pscustomobject]
实例通过
获取NcNode |选择对象…
管道成为其自己的
$nodes
元素,但是每个元素都作为一个元素子数组-因此需要额外的
[0]
索引访问。[1]


但是,它听起来像一个常规数组(
[array]
,实际上与
[object[]]
)在您的情况下是足够的,其中每个元素包含一个(单个标量)
[pscustomobject]

# Type constraint [array] creates a regular [object[]] array.
[array] $nodes = get-NcNode | select-object -property Node, @{Label = "slot"; expression = {@("a")*4}}
使用这样定义的
$nodes
,您的原始代码应该可以工作


[1] 由于PowerShell的功能,在获取值(而不是设置值)时,您可以不使用额外的索引就可以离开。

如果您确实需要一个数组数组(键入
[array[]]
),您的问题解决如下:

$nodes[0][0].slot[0] = "b" 
也就是说,每个
$nodes
元素本身就是一个数组,按照填充
$nodes
的方式,每个
[pscustomobject]
实例通过
获取NcNode |选择对象…
管道成为其自己的
$nodes
元素,但是每个元素都作为一个元素子数组-因此需要额外的
[0]
索引访问。[1]


但是,它听起来像一个常规数组(
[array]
,实际上与
[object[]]
)在您的情况下是足够的,其中每个元素包含一个(单个标量)
[pscustomobject]

# Type constraint [array] creates a regular [object[]] array.
[array] $nodes = get-NcNode | select-object -property Node, @{Label = "slot"; expression = {@("a")*4}}
使用这样定义的
$nodes
,您的原始代码应该可以工作



[1] 由于PowerShell的功能,在获取值时(而不是设置值时),您可以不使用额外的索引就离开。

$nodes[0]。插槽[0]=“b”
对我来说很好。请注意,
$nodes
数组未与
选择对象
表达式内联。如果插槽是数组,则显示输出中的字符串之间应该有空格:
{a,a,a}
。我建议您使用这个
$Nodes=1..4 | Select Object@{n='Node';e={nn0$},{n='Slot';e={('a')*4}}
作为输入。另外:初始化语句
[array[]$Nodes=@()
,是没有意义的:它的效果被下一个
[array[]$Nodes=.
语句替换。@iRon,只有当
[array[]]
更改为
[array]
$nodes[0]时,代码才起作用。插槽[0]=“b”
对我来说可以正常工作。请注意,
$nodes
数组未与
选择对象
表达式内联。如果插槽是数组,则显示输出中的字符串之间应该有空格:
{a,a,a}
。我建议您使用这个
$Nodes=1..4 | Select Object@{n='Node';e={nn0$},{n='Slot';e={('a')*4}}
作为输入。另外:初始化语句
[array[]$Nodes=@()
,是没有意义的:它的效果被下一个
[array[]$Nodes=.
语句替换。@iRon,仅当
[array[]]
更改为
[array]
时,代码才起作用。