gpt4 book ai didi

powershell - 为什么 PowerShell 以不同的方式考虑 @() 语法

转载 作者:行者123 更新时间:2023-12-03 14:12:48 25 4
gpt4 key购买 nike

数组初始化在 中的工作方式不同

$scripts = @(
("01", "a" , "01_Cleanup")
,("02", "b" , "02_Cleanup")
);

输出:

$scripts[0] - "01"
$scripts[1] - "a"
$scripts[2] - "01_Cleanup"
$scripts[3] - "02"
"b"
"02_Cleanup"

请注意数组初始化中的“,”:

$scripts = @(
("01", "a" , "01_Cleanup"),
("02", "b" , "02_Cleanup")
);

输出:

$scripts[0] - "01"
"a"
"01_Cleanup"
$scripts[1] - "02"
"b"
"02_Cleanup"

为什么?

最佳答案

这是因为 Comma operator 的用法不同。 (二进制 vs 一元):

As a binary operator, the comma creates an array. As a unary operator, the comma creates an array with one member. Place the comma before the member.


二进制:

@(
("01", "a" , "01_Cleanup"),
("02", "b" , "02_Cleanup")
) | foreach { Write-Host $_};

输出:

01 a 01_Cleanup
02 b 02_Cleanup

一元:

 @(
("01", "a" , "01_Cleanup")
,("02", "b" , "02_Cleanup")
) | foreach { Write-Host $_};

输出:

01
a
01_Cleanup
02 b 02_Cleanup

关于powershell - 为什么 PowerShell 以不同的方式考虑 @() 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com