Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Breaking out of FOREACH

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-20-2006   #1 (permalink)
John Smith
Guest


 

Breaking out of FOREACH

Is there a NEXT or CONTINUE or similar for the FOREACH loop?

How about BREAK?

Thanks.

My System SpecsSystem Spec
Old 12-21-2006   #2 (permalink)
Lucvdv
Guest


 

Re: Breaking out of FOREACH

On Wed, 20 Dec 2006 19:00:00 -0800, John Smith
<JohnSmith@discussions.microsoft.com> wrote:

> Is there a NEXT or CONTINUE or similar for the FOREACH loop?
>
> How about BREAK?


Not that I know of.

If you don't mind some complex and unreadable constructs, you can try to
simulate them through creative use of IF.


For example, to process the first five objects in the pipe (here it just
passes them through) and skip the rest as if there had been a break after
the fifth:

foreach { if($done -eq 0) { $_; $n=$n+1; if($n -eq 5) {$done=1} } } -begin
{ $n=0; $done=0 }

(that's all on one line)
My System SpecsSystem Spec
Old 12-21-2006   #3 (permalink)
RichS
Guest


 

Re: Breaking out of FOREACH

see

get-help about_break
get-help about_continue
--
Richard Siddaway

Please note that all scripts are supplied "as is" and with no warranty


"Lucvdv" wrote:

> On Wed, 20 Dec 2006 19:00:00 -0800, John Smith
> <JohnSmith@discussions.microsoft.com> wrote:
>
> > Is there a NEXT or CONTINUE or similar for the FOREACH loop?
> >
> > How about BREAK?

>
> Not that I know of.
>
> If you don't mind some complex and unreadable constructs, you can try to
> simulate them through creative use of IF.
>
>
> For example, to process the first five objects in the pipe (here it just
> passes them through) and skip the rest as if there had been a break after
> the fifth:
>
> foreach { if($done -eq 0) { $_; $n=$n+1; if($n -eq 5) {$done=1} } } -begin
> { $n=0; $done=0 }
>
> (that's all on one line)
>

My System SpecsSystem Spec
Old 12-21-2006   #4 (permalink)
Lucvdv
Guest


 

Re: Breaking out of FOREACH

On Thu, 21 Dec 2006 06:46:02 -0800, RichS <RichS@discussions.microsoft.com>
wrote:

> see
>
> get-help about_break
> get-help about_continue


<rant>

For as long as I've been using MS software (and that's since long before
they bought QDOS from Tim Patterson and renamed it to MS-DOS), I've always
wondered how people found out about these things. MS documentation is, and
has always been decent-at-best as a reference, but utterly useless for
learning.


Break and continue are definitely NOT documented in the places where you'd
expect them first, the Getting Started Guide and the PowerShell Primer.
There's a chapter about FOREACH in the primer, but it doesn't mention break
or continue. The word 'break' doesn't even occur in either of those two
documents, AT ALL.

The same with the online help for FOREACH: you would _at_the_very_least_
expect references in the "related links" section, but zippo. What you do
find there is a number of other cmdlets that have far less to do with
foreach-object, but that also happen to have 'object' as their noun.
Typical.


And to make it even worse, if you try by guessing because it simply 'has'
to exist:

C:\Temp\PowerShell> help break
Get-Help : Cannot find Help for topic "break".

</rant>
My System SpecsSystem Spec
Old 12-21-2006   #5 (permalink)
RichS
Guest


 

Re: Breaking out of FOREACH

If you've not seen the about documentation before it would be worth doing
somthing like

get-help about* | select name, synopsis | format-list

scroll down the list & see whats there

and of course

get-command

will give you a list of all the cmdlets available
--
Richard Siddaway

Please note that all scripts are supplied "as is" and with no warranty


"Lucvdv" wrote:

> On Thu, 21 Dec 2006 06:46:02 -0800, RichS <RichS@discussions.microsoft.com>
> wrote:
>
> > see
> >
> > get-help about_break
> > get-help about_continue

>
> <rant>
>
> For as long as I've been using MS software (and that's since long before
> they bought QDOS from Tim Patterson and renamed it to MS-DOS), I've always
> wondered how people found out about these things. MS documentation is, and
> has always been decent-at-best as a reference, but utterly useless for
> learning.
>
>
> Break and continue are definitely NOT documented in the places where you'd
> expect them first, the Getting Started Guide and the PowerShell Primer.
> There's a chapter about FOREACH in the primer, but it doesn't mention break
> or continue. The word 'break' doesn't even occur in either of those two
> documents, AT ALL.
>
> The same with the online help for FOREACH: you would _at_the_very_least_
> expect references in the "related links" section, but zippo. What you do
> find there is a number of other cmdlets that have far less to do with
> foreach-object, but that also happen to have 'object' as their noun.
> Typical.
>
>
> And to make it even worse, if you try by guessing because it simply 'has'
> to exist:
>
> C:\Temp\PowerShell> help break
> Get-Help : Cannot find Help for topic "break".
>
> </rant>
>

My System SpecsSystem Spec
Old 12-21-2006   #6 (permalink)
John Smith
Guest


 

Re: Breaking out of FOREACH

Thanks. I found HELP ABOUT_CONTINUE after posting the query. I was careful
about doing my due diligence before posting. I was searching for it under
HELP ABOUT_FOREACH. Even piped the output to a file and scanned it with a
word processor. I agree 100% with your rant. I would have written it myself.

My System SpecsSystem Spec
Old 12-21-2006   #7 (permalink)
John Smith
Guest


 

Re: Breaking out of FOREACH

One would expect CONTINUE to be defined in HELP ABOUT_FOREACH and HELP
ABOUT_FOR. Imagine ELSE is not mentioned in HELP ABOUT_IF but by itself
under HELP ABOUT_ELSE.

"RichS" wrote:

> If you've not seen the about documentation before it would be worth doing
> somthing like
>
> get-help about* | select name, synopsis | format-list
>


My System SpecsSystem Spec
Old 12-23-2006   #8 (permalink)
/\\/\\o\\/\\/ [MVP]
Guest


 

Re: Breaking out of FOREACH

if you try by guessing because it simply 'has'
>> to exist:


the first thing I would have tought of, and how I find out :-)

ls $PSHOME\en-US\*.txt | select-string 'Break'

ah why did I not think about that feeling ?

but serious, it would not be needed !, but still I do this a lot (where did
I find that info again ?).

Greetings /\/\o\/\/

"RichS" <RichS@discussions.microsoft.com> wrote in message
news:AE02045D-843E-42EF-9599-0991089D52E6@microsoft.com...
> If you've not seen the about documentation before it would be worth doing
> somthing like
>
> get-help about* | select name, synopsis | format-list
>
> scroll down the list & see whats there
>
> and of course
>
> get-command
>
> will give you a list of all the cmdlets available
> --
> Richard Siddaway
>
> Please note that all scripts are supplied "as is" and with no warranty
>
>
> "Lucvdv" wrote:
>
>> On Thu, 21 Dec 2006 06:46:02 -0800, RichS
>> <RichS@discussions.microsoft.com>
>> wrote:
>>
>> > see
>> >
>> > get-help about_break
>> > get-help about_continue

>>
>> <rant>
>>
>> For as long as I've been using MS software (and that's since long before
>> they bought QDOS from Tim Patterson and renamed it to MS-DOS), I've
>> always
>> wondered how people found out about these things. MS documentation is,
>> and
>> has always been decent-at-best as a reference, but utterly useless for
>> learning.
>>
>>
>> Break and continue are definitely NOT documented in the places where
>> you'd
>> expect them first, the Getting Started Guide and the PowerShell Primer.
>> There's a chapter about FOREACH in the primer, but it doesn't mention
>> break
>> or continue. The word 'break' doesn't even occur in either of those two
>> documents, AT ALL.
>>
>> The same with the online help for FOREACH: you would _at_the_very_least_
>> expect references in the "related links" section, but zippo. What you do
>> find there is a number of other cmdlets that have far less to do with
>> foreach-object, but that also happen to have 'object' as their noun.
>> Typical.
>>
>>
>> And to make it even worse, if you try by guessing because it simply 'has'
>> to exist:
>>
>> C:\Temp\PowerShell> help break
>> Get-Help : Cannot find Help for topic "break".
>>
>> </rant>
>>


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
LM Breaking Attachments Scott Live Mail 5 04-29-2008 08:39 PM
OT - Breaking News Mac Vista General 19 07-03-2007 02:52 PM
foreach, foreach-object & begin/process/end scriptblock clauses... Clint Bergman PowerShell 12 05-16-2007 05:30 PM
Difference in semantics of for, foreach and foreach-object Andrew Watt [MVP] PowerShell 3 01-26-2007 12:46 PM
possible bug with using the $foreach keyword inside foreach loops.. klumsy@gmail.com PowerShell 6 11-16-2006 06:50 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51