数组初始化在powershell
中的工作方式不同$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"
为什么?
答案 0 :(得分:8)
由于Comma operator(二进制与一元)的使用不同:
作为二元运算符,逗号创建一个数组。作为一元运算符, 逗号创建一个包含一个成员的数组。将逗号放在之前 构件。
<强>二进制:强>
@(
("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