powershell - 当我将结果存储到变量时,为什么会得到不同的结果?

标签 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

最佳答案

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

在第一个示例中,当您不将输出分配给变量时,您将按原样打印来自函数的数据。每次您发送 $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
  

关于powershell - 当我将结果存储到变量时,为什么会得到不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67542242/

相关文章:

arrays - 如何从远程pssession获取变量输出

powershell - 可以使用 PowerShell 添加 .inf 驱动程序吗?

powershell - 在 PowerShell Get-ChildItem -Exclude 不与 -Recurce 参数一起使用

python - 为什么 'python' 在 powershell 中无法识别?

powershell - 防止在批处理文件中解释多行命令

PostBuild 中的 PowerShell 脚本

PowerShell 飞溅不起作用

powershell - 复制项目将2条语句合并为1条

winforms - 将RTF加载到Winforms Richtextbox “in stages”

powershell - 获取返回的最后一个对象