PowerShell的数组初始化 [英] PowerShell array initialization

查看:371
本文介绍了PowerShell的数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是初始化PowerShell中的数组的最佳方式?

例如,code

  $阵列= @()
为($ I = 0; $ I -lt 5; $ I ++)
{
    $阵列[$ i] = $ FALSE
}

生成错误

 数组赋值失败,因为指数'0'是超出范围。
在H:\\ SOFTWARE \\ PowerShell的\\ TestArray.ps1:4个字符:10
+ $阵列[$<<<<我] = $ FALSE


解决方案

另一个选择:

 为($ 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.

这篇关于PowerShell的数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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