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

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

问题描述

我想创建一个包含两个数字数组的数组.很简单.但是,如果我不在第一个数组之前提供前导逗号,则是不正确的.为什么需要这个前导逗号?

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))) 输出一个数组,由两个整数组成.那是因为该语句中的前导逗号被解释为一元数组构造运算符(或逗号运算符),因此您将得到一个嵌套在另一个数组中的数组,并且在输出期间仅展开外部数组.

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.

本质上,你的表达是一样的

Essentially, your expression is the same as

$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天全站免登陆