为什么将结果存储到变量时会得到不同的结果?

时间:2021-05-15 00:20:51

标签: powershell

所以我试图编写一个简单的递归函数来创建集合的排列。 它创建了一个哈希表数组——每个表都是一个排列。 我进行了一个快速测试 - 成功了!

然后,我将结果存储在一个变量中,现在它不再起作用了。 结果变量中的所有行都与预期的最后一行相同。

为什么存储在变量中的结果不同? 这是由于某些优化导致的 powershell 错误吗?

function simple($data, $index)
{
    if ($index -lt 0) {
        $data.count = $global:count
        $global:count = $global:count + 1
        $data ### This is the return value
    } else {
        $name = "Name_$index"
        $data.$name = $index + 100
        simple $data ($index - 1)
        $data.$name = $index + 200
        simple $data ($index - 1)
    }
}

# Case1: Just let the results print - works as expected
$global:count = 0
$d = [ordered]@{Count = -1; Name_0 = -1; Name_1 = -1}
Write-Output "Case 1: Show return value"
simple $d 1

# Case2: Just let the results print - works as expected
$global:count = 0
$d = [ordered]@{Count = -1; Name_0 = -1; Name_1 = -1}
Write-Output "Case 2: Store then show return value"
$a = simple $d 1
$a

结果是:

Case 1: Show return value
Name                           Value
----                           -----
count                          0
Name_0                         100
Name_1                         101
count                          1
Name_0                         200
Name_1                         101
count                          2
Name_0                         100
Name_1                         201
count                          3
Name_0                         200
Name_1                         201


Case 2: Store then show return value
count                          3
Name_0                         200
Name_1                         201
count                          3
Name_0                         200
Name_1                         201
count                          3
Name_0                         200
Name_1                         201
count                          3
Name_0                         200
Name_1                         201

1 个答案:

答案 0 :(得分:3)

问题在于如何将数据发送出函数。同一个对象被多次输出。

在第一个示例中,当您不将输出分配给变量时,您将按原样打印来自函数的数据。每次您发送 $data 时,它都会打印出 $data 从您的函数中发送出去时的状态。

当您将输出分配给变量时,发生的情况是您将 $data 输出到变量,但当时您看不到这些值,因此您看不到它们当时是什么。然后你更新同一个对象 $data 的值并再次输出它并再次存储在变量中,但它们是同一个对象,所以现在以前的输出和新的输出是相同的。这还在继续。当您输出恰好是数组的 $a 变量时,它输出的 $data 对象可能会与您上次发送给它的任何值一起存储。

希望这是有道理的。也许这会有所帮助

function Test-HashUpdate {
    param (
        $hash
    )

    $hash.count++
    Write-Output $hash
    $hash.count++
    Write-Output $hash
}

function Test-HashUpdateClone {
    param (
        $hash
    )

    $hash.count++
    Write-Output $hash.Clone()
    $hash.count++
    Write-Output $hash.Clone()
}

$hash = @{count = 1 }
$a = test-HashUpdate $hash
$b = Test-HashUpdateClone $hash

Write-host "Hashtable outputted without clone"
$a
Write-host "`nHashtable outputted with clone"
$b

输出

Hashtable outputted without clone

Name                           Value
----                           -----
count                          5
count                          5

Hashtable outputted with clone
count                          4
count                          5
  
相关问题