PowerShell中嵌套的ForEach()

时间:2013-12-31 11:27:02

标签: powershell foreach break

我在Powershell中遇到了嵌套ForEach循环的麻烦。首先,我需要遍历列表1.对于列表1中的每个对象,我需要遍历列表2.当我在列表2中找到类似对象时,我想转到列表1中的下一个对象。

我试过休息,我已经尝试过继续,但它不会为我工作。

Function checkLists() {
  ForEach ($objectA in $listA) {
    ForEach ($objectB in $listB) {
       if ($objectA -eq $objectB) {
           // Do something 
           // goto next object in list A and find the next equal object
       }
    }
  }
}

a)在PowerShell中中断/继续做什么?

b)我应该如何解决我的问题'

4 个答案:

答案 0 :(得分:10)

使用get-help about_break中描述的标签:

A Break statement can include a label. If you use the Break keyword with
a label, Windows PowerShell exits the labeled loop instead of exiting the
current loop

像这样,

foreach ($objectA in @("a", "e", "i")) {
    "objectA: $objectA"
    :outer
    foreach ($objectB in @("a", "b", "c", "d", "e")) {
       if ($objectB -eq $objectA) {
           "hit! $objectB"
           break :outer
       } else { "miss! $objectB" }
    }
}

#Output:
objectA: a
hit! a
objectA: e
miss! a
miss! b
miss! c
miss! d
hit! e
objectA: i
miss! a
miss! b
miss! c
miss! d
miss! e

答案 1 :(得分:4)

这是使用break / continue的示例。在内循环中反转测试,并使用Continue继续循环,直到测试失败。一旦它被击中,它将打破内部循环,并返回到外部循环中的下一个对象。

foreach ($objectA in @("a", "e", "i"))
   {
    "objectA: $objectA"
    foreach ($objectB in @("a", "b", "c", "d", "e")) {
       if ($objectB -ne $objectA)
         {
           "miss! $objectB"
           continue
         }
     else {
           "hit!  $objectB" 
           break
          }
   }
}

objectA: a
hit!  a
objectA: e
miss! a
miss! b
miss! c
miss! d
hit!  e
objectA: i
miss! a
miss! b
miss! c
miss! d
miss! e

答案 2 :(得分:1)

我会使用Do..until循环 - 它的用途正是你所描述的。

Function checkLists() {
  ForEach ($objectA in $listA) {
    $Counter = 0
    Do {
        $ObjectB = $listB[$Counter]
        #other stuff
    }
    #Keep going until we have a match or reach end of array
    Until (($objectA -eq $objectB) -or (($Counter+1) -eq $Listb.count()))
    }
}

这是一个简单的例子:

#Example use of do..until
$i = 1
do {
  $i++
  write-host $i
  }
until ($i -eq 10)

答案 3 :(得分:-1)

这个花了我一段时间才最终弄清楚了。希望解决方案和示例能够帮助其他人。

在这个应用程序中,我需要将每个节点和子节点作为嵌套循环进行处理。关键似乎是在foreach循环中获取相对节点名称。

以下是代码:

[System.Xml.XmlDocument] $xml = new-object System.Xml.XmlDocument;
$xml.load("g:\tmp\down\order_01.xml");

foreach ( $order in $xml.SelectNodes("//orders/order"))
{
    # all these work
    $invoiceNumber = $xml.orders.order."invoice-no";                 
    $invoiceNumber = $order."invoice-no";                            
    $invoiceNumber = $order.SelectSingleNode("//invoice-no");        
    $invoiceNumber = $order.SelectSingleNode("./invoice-no");        
    $invoiceNumber = $order.SelectSingleNode("invoice-no");          

    foreach ($lineItem in $order.SelectNodes("product-lineitems/product-lineitem"))   #$xml.orders.order.'product-lineitems'.'product-lineitem')
    {
        $tax = $lineItem.tax;                             #works
        $tax = $lineItem.SelectSingleNode("tax");         #also works

        $name = $lineItem."product-name";
        write-host "Tax ", $tax, " name ", $name;

        foreach ( $optionItem in $lineItem.SelectNodes("option-lineitems/option-lineitem"))
        {
            $optionTax = $optionItem."tax";
            $optionId  = $optionItem."option-id";
            if ($optionId -eq 'engravingOption')
            {
#                $optionTax = $optionItem.SelectSingleNode("tax");
                Write-Host  "option Tax ",  $optionTax, " option id ", $optionId;
            }
        }
    }
}


And here is the sample xml:


<?xml version="1.0" encoding="UTF-8"?>      
<orders>xmlns="http://www.demandware.com/xml/impex/order/2006-10-31">
    <order order-no="00030562">     
        <invoice-no>00038012</invoice-no>   
        <product-lineitems> 
            <product-lineitem>      
                <tax>13.45</tax>    
                <product-name>Scoop Ice Bucket</product-name>       
                <option-lineitems>  
                    <option-lineitem>       
                        <net-price>7.00</net-price> 
                        <tax>11.11</tax>     
                        <option-id>includeGiftWrapping</option-id>  
                    </option-lineitem>      
                </option-lineitems> 
            </product-lineitem>     
            <product-lineitem>      
                <tax>22.22</tax>     
                <product-name>Love Bowl</product-name>      
                <option-lineitems>  
                    <option-lineitem>       
                        <tax>33.33</tax>     
                        <option-id>includeGiftWrapping</option-id>  
                    </option-lineitem>      
                    <option-lineitem>       
                        <tax>44.44</tax>     
                        <option-id>includeGiftWrapping</option-id>  
                    </option-lineitem>      
                </option-lineitems> 
            </product-lineitem>     
            <product-lineitem>      
                <tax>55.55</tax>    
                <product-name>Groove Decanter Set</product-name>    
                <option-lineitems>  
                    <option-lineitem>       
                        <tax>66.66</tax>     
                        <option-id>includeGiftWrapping</option-id>  
                    </option-lineitem>      
                </option-lineitems> 
            </product-lineitem>     
            <product-lineitem>      
                <tax>66.66</tax>     
                <lineitem-text>Klasp Cocktail Shaker</lineitem-text>
                <option-lineitems>  
                    <option-lineitem>       
                        <tax>77.77</tax>     
                        <option-id>includeGiftWrapping</option-id>  
                    </option-lineitem>      
                    <option-lineitem>       
                        <tax>88.88</tax>     
                        <option-id>engravingOption</option-id>      
                    </option-lineitem>      
                </option-lineitems> 
            </product-lineitem>     
        </product-lineitems>
    </order>
</orders>