Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何将数组参数传递给函数_Powershell - Fatal编程技术网

Powershell 如何将数组参数传递给函数

Powershell 如何将数组参数传递给函数,powershell,Powershell,我在PowerShell中实现了一个简单的合并排序函数 function Merge-Sort { param($a) if ($a.Length -gt 1) { $m = [Math]::floor($a.Length / 2) [Int[]]$l = $a[0..($m-1)] [Int[]]$r = $a[$m..($a.Length)] Merge-Sort $l Merge-Sort $r $i = $j = $k =

我在PowerShell中实现了一个简单的合并排序函数

function Merge-Sort
{
  param($a)
  if ($a.Length -gt 1)
  {
    $m = [Math]::floor($a.Length / 2)
    [Int[]]$l = $a[0..($m-1)]
    [Int[]]$r = $a[$m..($a.Length)]

    Merge-Sort $l
    Merge-Sort $r

    $i = $j = $k = 0

    while ($i -lt $l.Length -and $j -lt $r.Length)
    {
      if ($l[$i] -lt $r[$j])
      {
        $a[$k] = $l[$i]
        $i++
      }
      else
      {
        $a[$k] = $r[$j]
        $j++
      }
      $k++
    }

    while($i -lt $l.length)
    {
        $a[$k] = $l[$i]
        $i++
        $k++
    }

    while($j -lt $r.length)
    {
        $a[$k] = $r[$j]
        $j++
        $k++
    }
 }
}
函数执行其应执行的操作,并对整数值数组进行排序:

 $arr = @(22,44,55,11,66,11,77,99,33,88)
 merge-sort $arr
输出为:111223445566778899

但是,当我将函数参数定义为[Int[],以明确它应该是一个整数数组而不是对象时,出现了一些错误,数组保持未排序:

 function Merge-Sort
 {
    param([Int[]]$a)
    ...
 }
输出为:22 44 55 11 66 11 77 99 33 88

我的问题是:


为什么定义函数参数的正确方法会导致不正确的结果(数组没有被排序)?

当创建对象$arr时,因为它没有定义为int数组[int[],而是创建为[array]。最初在添加[int[]之前传递给函数时。它传递了引用并更改了数据。如果向数组中添加了一个addition对象,它将不会返回任何内容,因为这样会创建一个新的数组对象

将[int[]]添加到参数时,它创建了一个名为$a的全新int数组[int[]]对象,并更改了其中的数据。由于$a从不返回,因此变量在函数末尾被终止

如果您传递了一个int数组,它将操纵该数组

让我们看几个例子

这将把索引0处的第一个值更改为5。由于没有向数组中添加对象,并且数组可以是参数中的任何对象,因此允许更改对索引0的引用,而不是需要返回一个全新的对象

function TestFunction($a)
{
    $a[0] = 5
}
$TestVar = @(2,3,1)
TestFunction -a $TestVar
$TestVar
输出5,3,1

因为原始对象是数组类型,参数是int数组类型。该函数将基于输入数组创建一个新的int数组。由于函数需要返回新的int数组,并且没有返回,因此它将被垃圾收集

function TestFunction([int[]]$a)
{
    $a[0] = 4
}
$TestVar = @(2,3,1)
TestFunction -a $TestVar
$TestVar
所以在你的情况下我们可以

function TestFunction([int[]]$a)
{
  $a[0] = 4
}
[int[]]$TestVar = @(2,3,1)
TestFunction -a $TestVar
$TestVar

由于参数正在查找int数组,并且输入是int数组,并且没有从数组中添加或减去新对象,因此将正确更改值。

之所以发生这种情况,是因为在第1个版本中,您是通过引用而不是通过值传入数组。第二个版本按值传入项。。。并且您不发送任何输出。[grin]////还有,你为什么要用那种非常奇怪的方式进行分类?有一个内置的
[array]::Sort()
方法将对数组进行就地排序。还有一个
[array]::Reverse()
在里面,用来发出咯咯的笑声。[咧嘴笑]不同的通话技巧很好-我不确定。这个函数只是我的算法课的一种训练练习。因此,没有返回值是故意的。通常我会使用类似Sort-Object的东西。谢谢你的“为什么”。ArcSet提供的详细(而且更准确)解释非常好。。。明显比我的好。回答得好!我曾考虑使用[Ref],但您的解决方案看起来更简单、更可靠。因此,最后我只是忘了将数组显式声明为Int[]]。。。