Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gotcha :: Breaking from the pipeline #138

Open
JohnLBevan opened this issue Aug 12, 2019 · 3 comments
Open

Add Gotcha :: Breaking from the pipeline #138

JohnLBevan opened this issue Aug 12, 2019 · 3 comments

Comments

@JohnLBevan
Copy link

JohnLBevan commented Aug 12, 2019

It would be good to add a gotcha on breaking from pipelines...

i.e. People expect this:

    $found = $false
    $list | ForEach-Object {
        if ($_ -eq $searchFor) {
            $found = $true
            break
        }
    }
    if ($found){'Woo'}else{'Boo'}

to work like this:

    $found = $false
    foreach {$item in $list} {
        if ($item -eq $searchFor) {
            $found = $true
            break
        }
    }
    if ($found){'Woo'}else{'Boo'}
@Bill-Stewart
Copy link

Agree; this behavior is pretty well-known, but may cause confusion and is not intuitive. Normally, if I need break or continue in a loop, I use a language keyword (for, foreach, while, etc.) instead of ForEach-Object to avoid confusion.

@Objections
Copy link

Maybe add a counter to show that they are different as you can't see it on the output in this case.
Where should it be added? Under pipeline maybe?

@serjou1
Copy link

serjou1 commented Jun 17, 2020

You should use return inside ForEach-Object instead of break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants