Powershell goto语句替换
Powershell goto statement replacement
我知道 powershell 没有 goto 语句。我有如下要求:
foreach($x in $xyz)
{
#something works fine
if($a -eq $b)
{
#something works fine
if($c -eq $d)
{
$c | Add-Content "path\text.txt"
}else
{
#Here I need to go to the first if statement, rather than going to foreach loop
}
}else
{
#nothing
}
}
尝试过 break :---- 和功能,但两者似乎都不适用于我的情况。当我使用 break :---- 时,它会在最后一个 else 中通知错误。当我尝试使用如下功能时:
foreach($x in $xyz)
{
#something works fine
function xyz
{
if($a -eq $b)
{
#something works fine
if($c -eq $d)
{
$c | Add-Content "path\text.txt"
}else
{
xyz
}
}else
{
#nothing
}
}
}
它没有进入函数xyz。
还有其他实现此目的的建议吗?非常感谢任何帮助。
我想你想要的是嵌套循环:
foreach ($item in $data) {
while (condition $data) {
# Do stuff, calculate $state
if (otherCondition($state)) {
# without label will exit in inner loop only,
# execution will continue at Point-X.
break;
}
# more stuff
}
# Point-X
}
我知道 powershell 没有 goto 语句。我有如下要求:
foreach($x in $xyz)
{
#something works fine
if($a -eq $b)
{
#something works fine
if($c -eq $d)
{
$c | Add-Content "path\text.txt"
}else
{
#Here I need to go to the first if statement, rather than going to foreach loop
}
}else
{
#nothing
}
}
尝试过 break :---- 和功能,但两者似乎都不适用于我的情况。当我使用 break :---- 时,它会在最后一个 else 中通知错误。当我尝试使用如下功能时:
foreach($x in $xyz)
{
#something works fine
function xyz
{
if($a -eq $b)
{
#something works fine
if($c -eq $d)
{
$c | Add-Content "path\text.txt"
}else
{
xyz
}
}else
{
#nothing
}
}
}
它没有进入函数xyz。
还有其他实现此目的的建议吗?非常感谢任何帮助。
我想你想要的是嵌套循环:
foreach ($item in $data) {
while (condition $data) {
# Do stuff, calculate $state
if (otherCondition($state)) {
# without label will exit in inner loop only,
# execution will continue at Point-X.
break;
}
# more stuff
}
# Point-X
}