Powershell goto语句替换

标签 powershell goto

我知道 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语句替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654602/

相关文章:

powershell - 从Windows Powershell ISE运行PHP CLI

excel - 使用 Powershell 添加新列并填充工作表名称

java - Java 中 goto 语句的替代方案

c++ - C 或 C++ 中好的 goto 示例

fortran - 适用于非常旧的代码的 ifort 方言选项

.net - 如何从powershell构建VS解决方案文件夹

powershell - 如何通过powershell脚本获取本地用户的组?

PowerShell 多重捕获问题

c++ - 将分屏多人游戏添加到 C++ 游戏

c# - 有人还在 C# 中使用 [goto] 吗?如果是,为什么?