簡體   English   中英

PowerShell中嵌套的ForEach()

[英]Nested ForEach() in PowerShell

我在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)我應該如何誇大我的'問題'?

使用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

這是使用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

我會使用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)

這個花了我一段時間才最終弄清楚了。 希望解決方案和示例能夠幫助其他人。

在這個應用程序中,我需要將每個節點和子節點作為嵌套循環進行處理。 關鍵似乎是在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>      

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM