I'm having some troubles with nested ForEach loops in Powershell. First, I need to iterate through list 1. For every object in list 1, I need to iterate through list 2. When I found the resembling object in list 2, I want to go to the next object in list 1.
I've tried break, i've tried continue, but it won't work for me.
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) What does a break/continue exactly do in PowerShell?
b) How exaclty should I conquer my 'problem'?
Nesting ForEach Tags consists of creating two loops, an inner loop and an outer loop, such that for each item returned by the outer loop, the inner loop executes completely and returns its items associated to the outer loop's item.
The ForEach-Object cmdlet performs an operation on each item in a collection of input objects. The input objects can be piped to the cmdlet or specified using the InputObject parameter. Starting in Windows PowerShell 3.0, there are two different ways to construct a ForEach-Object command. Script block.
Using break in loopsWhen a break statement appears in a loop, such as a foreach , for , do , or while loop, PowerShell immediately exits the loop. A break statement can include a label that lets you exit embedded loops. A label can specify any loop keyword, such as foreach , for , or while , in a script.
Use a label as described in 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
Like so,
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
Here's an example using break/continue. Reverse the test in the inner loop, and use Continue to keep the loop going until the test fails. As soon as it gets a hit, it will break the inner loop, and go back to the next object in the outer loop.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With