PowerShell不会返回一个空数组作为一个数组 [英] PowerShell doesn't return an empty array as an array

查看:405
本文介绍了PowerShell不会返回一个空数组作为一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这个作品:

$ar = @()
$ar -is [Array]
  True

为什么不这项工作?

Why doesn't this work?

function test {
    $arr = @()
    return $arr
}

$ar = test
$ar -is [Array]
  False

这就是为什么没有一个空数组从测试函数返回?

That is, why isn't an empty array returned from the test function?

推荐答案

您功能不起作用,因为PowerShell中返回所有<一个href=\"http://rkeithhill.word$p$pss.com/2007/09/16/effective-powershell-item-7-understanding-output/\">non-captured流输出,在收益语句不只是说法。一个空数组是在这个过程中错位成 $空。但是,您可以preserve由<返回数组href=\"http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx\">$p$ppending它与数组建设运营(

Your function doesn't work because PowerShell returns all non-captured stream output, not just the argument of the return statement. An empty array is mangled into $null in the process. However, you can preserve an array on return by prepending it with the array construction operator (,):

function test {
  $arr = @()
  return ,$arr
}

这篇关于PowerShell不会返回一个空数组作为一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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