为什么在创建数组时需要逗号前导? [英] Why is a leading comma required when creating an array?

查看:138
本文介绍了为什么在创建数组时需要逗号前导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含两个数字的数组的数组.非常简单.但是,如果我没有在第一个数组前提供前导逗号,那是不正确的.为什么需要这个逗号开头?

I want to create an array containing arrays of two numbers. Pretty straightforward. However, If I do not provide a leading comma before the first array, it is incorrect. Why is this leading comma required?

PS C:\src\powershell> Get-Content .\fr-btest.ps1
$files1 = @(
@(4, 1024)
, @((7), (16))
)

$files1
$files1.GetType()
$files1.Length
$files1.Count
'========'

$files2 = @(
, @(4, 1024)
, @((7), (16))
)

$files2
$files2.GetType()
$files2.Length
$files2.Count

PS C:\src\powershell> .\fr-btest.ps1
4
1024
7
16

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
3
3
========
4
1024
7
16
True     True     Object[]                                 System.Array
2
2

推荐答案

@()

@() is the array subexpression operator, which works differently than array construction operators you may be used to from other languages. The operator evaluates the nested subexpression and returns the output of that expression as an array. Meaning you can do something like this:

@(
Write-Output 'foo'
Get-Content 'C:\some\file.txt'
Test-Connection '192.168.23.42' -Count 1
)

有一个数组出来.

对于您的第一个示例,这意味着分别对两个语句@(4, 1024), @((7), (16))求值,然后将两个语句的集合输出作为数组返回.

For your first example this means that the two statements @(4, 1024) and , @((7), (16)) are evaluated individually, and the collective output of the two statements is then returned as an array.

第一个语句(@(4, 1024))输出两个整数,但是第二个语句(, @((7), (16)))输出两个整数的 array .那是因为该语句中的前导逗号被解释为一元数组构造运算符(或逗号运算符),因此您将嵌套一个数组嵌套在另一个数组中,并且在输出期间仅展开外部数组.

The first statement (@(4, 1024)) outputs two integers, but the second statement (, @((7), (16))) outputs an array of two integers. That is because the leading comma in that statement is interpreted as the unary array construction operator (or comma operator), so you get an array nested in another array, and only the outer array is unrolled during output.

本质上,您的表情与

$files1 = @(
4
1024
, @(7, 16)
)

$files1 = 4, 1024, @(7, 16)

您的第二个示例避免了这种陷阱,因为两个嵌套数组都以一元数组构造运算符为前缀,因此可以防止其被完全展开.

Your second example avoids this pitfall, because both nested arrays are prepended with the unary array construction operator and thus protected from being completely unrolled.

话虽如此,我建议以更明确的方式定义数组,例如像这样:

With that said, I would recommend to define arrays in a more clear-cut way, e.g. like this:

$files1 = @(4, 1024),
          @(7, 16)

或(使用分组表达式而不是数组子表达式)如下:

or (using grouping expressions instead of array subexpressions) like this:

$files1 = (4, 1024),
          (7, 16)

避免像您观察到的那样的意外.外部@()在这里定义数组不是必需的. PowerShell通过第一行末尾的逗号自动检测到这一点.

to avoid surprises like the one you observed. The outer @() isn't necessary for defining an array here. PowerShell automatically detects that via the trailing comma at the end of the first line.

有关更多信息,请参见 about_Operators .

For further information see about_Operators.

这篇关于为什么在创建数组时需要逗号前导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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