问题描述
好的.我正在尝试完成一项学校作业,但终生无法弄清楚这一点.我正在尝试使用 powershell 将值从一个函数传递到另一个函数,从而制作模块化"类型脚本.我似乎无法弄清楚如何在不使用 $script:xxxxx 的情况下将值移出函数的范围.是否有另一种方法可以将 powershell 中的值作为按引用传递的常规参数参数移动?
Alright. I'm attempting to complete a school assignment and cannot for the life of me figure this out. I'm trying to use powershell to pass values from one function to another making a "modular" type script. I can't seem to figure out how to move the values out of the scope of the function without using the $script:xxxxx. Is there another way to move the values in powershell as a regular argument parameter pass by reference?
这是我所拥有的:
function main
{
inputGrams($carbGrams, $fatGrams)
$carbGrams
$carbGrams
calcGrams
displayInfo
}
function inputGrams([ref]$carbGrams, [ref]$fatGrams)
{
$carbGrams = read-host "Enter the grams of carbs per day"
$fatGrams = read-host "Enter the grams of fat per day"
}
function calcGrams
{
$carbCal = $carbGrams * 4
$fatCal = $fatGrams * 9
}
function displayInfo
{
write-host "The total amount of carb calories is $carbCal"
write-host "The total amount of fat calories is $fatCal"
}
main
inputGrams 函数之后的两个值应该在每次运行脚本时更改,但它们不会因为范围问题和传递值而发生变化.有人知道如何将这些值正确地传递回主函数吗?
The two values right after the inputGrams function should change each time the script is run but they don't because of scope issues and passing the values. Anyone know how to properly pass those values back to the main function?
推荐答案
Andy 走在正确的道路上,但 [Ref] 很棘手,如果可以,建议避免它们.
Andy is on the right path but [Ref] are tricky and it is recommended to avoid them if you can.
正如您所说,问题在于范围.你的所有函数——除了main——都是从main调用的,因此你需要通过在main的作用域(即它们的父作用域)中设置变量来使这些函数可用, 使用 Set-Variable 或 New-Variable.
As you said, the problem is scope. All your functions —except main— are called from main, therefore you need to make the variables available to these functions by setting them in main's scope, i.e. their parent scope, with Set-Variable or New-Variable.
当使用 Get-Variable 检索它们的值时,同样的点是有效的.
Same point is valid when retrieving their values with Get-Variable.
function main
{
inputGrams
$carbGrams
$fatGrams
calcGrams
displayInfo
}
function inputGrams
{
# type constrain as Single because Read-Host returns a String
[single]$carbs = read-host "Enter the grams of carbs per day"
[single]$fat = read-host "Enter the grams of fat per day"
# scope 1 is the parent scope, i.e. main's scope
Set-Variable -Name carbGrams -Value $carbs -Scope 1
Set-Variable -Name fatGrams -Value $fat -Scope 1
}
function calcGrams
{
# scope 1 is the parent scope, i.e. main's scope
Set-Variable -Name carbCal -Value ($carbGrams * 4) -Scope 1
Set-Variable -Name fatCal -Value ($fatGrams * 9) -Scope 1
}
function displayInfo
{
# scope 1 is the parent scope, i.e. main's scope
$_carbCal = Get-Variable -Name carbCal -Scope 1 -ValueOnly
$_fatCal = Get-Variable -Name fatCal -Scope 1 -ValueOnly
write-host "The total amount of carb calories is $_carbCal"
write-host "The total amount of fat calories is $_fatCal"
}
main
PS:我希望我没有破坏你的作业,只是想帮忙;)
PS: I hope I did not spoil your school assignment, just wanted to help out ;)
这篇关于Powershell 将参数值传递给参数并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!