PowerShell枚举一个仅包含一个内部数组的数组 [英] PowerShell enumerate an array that contains only one inner array

查看:50
本文介绍了PowerShell枚举一个仅包含一个内部数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下:这是一个数组,仅包含一项.数组的项是另一个数组(我称其为列表对象).我想获得数组内的唯一列表对象.

Conside this: This is one array and contains only one item. And the item of the array is another array(I call it list object). I want to get the only list object inside an array.

我使用foreach来获取列表,但是它将列表对象视为数组并返回列表对象的第一项.

I use foreach to get the list, but it treates the list object as an array and return the first item of the list object.

$OSName1 = @(
    @("win2008r2")
)
$OSName2 = @(
    @("win2008r2"),
    @("win2012")
)


foreach ($osname in $OSName1) {
    write-host $osname[0]
}

foreach ($osname in $OSName2) {
    write-host $osname[0]
}

我希望结果的输出是:win2008r2win2008r2win2012

I expect the output of result is: win2008r2 win2008r2 win2012

但是真正的结果是wwin2008r2win2012为什么powershell会自动将内部数组扩展为其父级?

But the real result is w win2008r2 win2012 Why powershell auto expands the inside array to it parent?

推荐答案

您通常会误解数组构造,尤其是 @()运算符在PowerShell中的工作方式.如果您看一下2个数组变量的值,您会注意到只有第二个数组变量具有嵌套数组:

You're misunderstanding how array construction in general and the @() operator in particular work in PowerShell. If you take a look at the value of your 2 array variables you'll notice that only the second one has nested arrays:


PS C:\> ConvertTo-Json $OSName1
[
    "win2008r2"
]
PS C:\> ConvertTo-Json $OSName2
[
    [
        "win2008r2"
    ],
    [
        "win2012"
    ]
]

这是因为数组子表达式运算符 @()计算嵌套表达式,然后将结果作为数组返回.但是,当您将一个数组子表达式嵌套到另一个数组子表达式中时,内部子表达式的结果会在评估外部子表达式时自动展开.因此,您的第一个变量变为 ['win2008r2'] 而不是预期的 [['win2008r2']] .

That is because the array subexpression operator @() evaluates the nested expression and then returns the result as an array. But when you're nesting an array subexpression into another array subexpression the result of the inner subexpression is automatically unrolled upon the evalution of the outer subexpression. Because of that your first variable becomes ['win2008r2'] instead of the intended [['win2008r2']].

您的第二个示例按预期方式工作,因为外部数组子表达式不仅包含嵌套数组子表达式,还包含嵌套子表达式的 array :

Your second example works the way you expect because the outer array subexpression contains not just a nested array subexpression, but an array of nested subexpressions:

@(...), @(...)
      ^
       `- this comma is what actually creates the array of arrays

外部数组子表达式仅展开外部数组,因此最终结果仍然是数组数组.基本上,您不需要外部的 @(())即可获得所需的结果.删除它,您将得到完全相同的结果:

The outer array subexpression unrolls only the outer array, so that the result is still an array of arrays in the end. Basically, you don't need the outer @() for the desired result. Remove it and you will get the exact same result:

$OSName2 = @("win2008r2"), @("win2012")

要仅使用一个嵌套数组来获得相似的结果,则需要使用一元数组构造运算符:

To get a similar result with just a single nested array you need to use the unary array construction operator:

$OSName1 = ,@("win2008r2")

这篇关于PowerShell枚举一个仅包含一个内部数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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