Powershell:二维数组 [英] Powershell: Two dimension arrays

查看:549
本文介绍了Powershell:二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下按预期工作:

$values = @( ("a", "b"), ("c", "d") )
foreach($value in $values)
{
  write-host "Value 0 =" $value[0]
  write-host "Value 1 =" $value[1]
}

其结果是(1):

Value 0 = a
Value 1 = b
Value 0 = c
Value 1 = d

但是如果我将$ values变量更改为:

$values = @( ("a", "b") )

结果(2)为:

Value 0 = a
Value 1 =
Value 0 = b
Value 1 =

我本来希望result(3)是:

Value 0 = a
Value 1 = b

将$ value更改为:

$values = @( ("a"), ("b") )

得到与上面的result(2)相同的结果.这些是非常不同的数据表示形式.

我正在编写的脚本需要能够处理二维数组,其中第一维的长度是从0到N.我希望能够编写脚本,以便如果第一级元素需要被添加(或删除)了我不必更改脚本的逻辑;我希望能够只编辑数据".

所以我的问题是:当数组的第一维长度为1时,如何注释二维数组,以便显示的 foreach 循环正确运行?

get-host响应:

Name             : ConsoleHost
Version          : 2.0
InstanceId       : 2338657f-e474-40d8-9b95-7e2b5f6a8acf
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

解决方案

这是PowerShell的数组处理行为可能导致意外结果的另一种情况.

我认为您需要使用逗号技巧(数组运算符)才能获得所需的结果:

$values = @( ,("a", "b") )
foreach($value in $values)
{
  write-host "Value 0 =" $value[0]
  write-host "Value 1 =" $value[1]
}

结果:

Value 0 = a
Value 1 = b

实际上您需要的是这个

$values = ,("a", "b")

本文详细介绍了PowerShell对数组的处理:

http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx

The following works as expected:

$values = @( ("a", "b"), ("c", "d") )
foreach($value in $values)
{
  write-host "Value 0 =" $value[0]
  write-host "Value 1 =" $value[1]
}

which results(1) in:

Value 0 = a
Value 1 = b
Value 0 = c
Value 1 = d

But if I change the $values variable to:

$values = @( ("a", "b") )

the result(2) is:

Value 0 = a
Value 1 =
Value 0 = b
Value 1 =

whereas I would have expected the result(3) to be:

Value 0 = a
Value 1 = b

Changing the $value to:

$values = @( ("a"), ("b") )

gives the same result as result(2) above. These are very different data representations.

The script that I am writing needs to be able to handle two-dimensional arrays where the first dimension has length from 0 thru N. I would like to be able to write the script so that if a first-level element needs to be added (or removed) that I don't have to change the logic of the script; I'd like to be able to just edit the "data".

So my question is: How do I notate a two-dimensional array so the shown foreach loop will work correctly when the first dimension of the array has a length of 1?

get-host responds with:

Name             : ConsoleHost
Version          : 2.0
InstanceId       : 2338657f-e474-40d8-9b95-7e2b5f6a8acf
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

解决方案

This is another case where PowerShell's array handling behavior may cause unexpected results.

I think you'll need to use the comma trick (the array operator) to get the desired result:

$values = @( ,("a", "b") )
foreach($value in $values)
{
  write-host "Value 0 =" $value[0]
  write-host "Value 1 =" $value[1]
}

Result:

Value 0 = a
Value 1 = b

Actually all you need is this:

$values = ,("a", "b")

This article explains more about PowerShell's handling of arrays:

http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx

这篇关于Powershell:二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆