且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell的数组初始化

更新时间:2023-11-08 12:54:04

另一个选择:

 为($ I = 0; $ I -lt 5; $ I ++)
{
  $ ARR + = @($ FALSE)
}

如果$ ARR还没有定义这一件作品。

What's the best way to initialize an array in PowerShell?

For example, the code

$array = @()
for($i=0; $i -lt 5;$i++)
{
    $array[$i] = $FALSE
}

generates the error

Array assignment failed because index '0' was out of range.
At H:\Software\PowerShell\TestArray.ps1:4 char:10
+         $array[$ <<<< i] = $FALSE

Yet another alternative:

for ($i = 0; $i -lt 5; $i++) 
{ 
  $arr += @($false) 
}

This one works if $arr isn't defined yet.