![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Why can't a foreach block begin a pipeline? Although some of the constraints on the internal foreach construct exist for understandable reasons, I can't see the reason that output generated within it is not treated as an output stream directly. Can anyone explain this? For example, the following causes an error: PS> foreach($i in 1,2,3){$i} | gm An empty pipe element is not permitted. At line:1 char:27 + foreach($i in 1,2,3){$i} | <<<< gm I could see this to overcome potential difficulties if you could move the enumerator backwards, but you can't. Even if you could, you could still forestall any problems with releasing objects by simply completing the block before emitting the output. The following _does_ work and takes minimal extra effort, so this isn't a major inconvenience; I'm just curious about why it is necessary: PS> $(foreach($i in 1,2,3){$i}) | gm |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Why can't a foreach block begin a pipeline? The grammar for pipeline statement is that it is made up of a list of expression or command connected with pipes. Foreach, on the other hand, is a statement. So the command below is eventually the same as following two commands, foreach($i in 1,2,3){$i} | gm The reason for this behaviour is purely a design decision so that clean cut between different layers of language structure. -- George Xie [MSFT] Microsoft Command Shell Development Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message news:%23W5sPUE%23GHA.4376@TK2MSFTNGP03.phx.gbl... > Although some of the constraints on the internal foreach construct exist > for understandable reasons, I can't see the reason that output generated > within it is not treated as an output stream directly. Can anyone explain > this? > > For example, the following causes an error: > PS> foreach($i in 1,2,3){$i} | gm > An empty pipe element is not permitted. > At line:1 char:27 > + foreach($i in 1,2,3){$i} | <<<< gm > I could see this to overcome potential difficulties if you could move the > enumerator backwards, but you can't. Even if you could, you could still > forestall any problems with releasing objects by simply completing the > block before emitting the output. > > The following _does_ work and takes minimal extra effort, so this isn't a > major inconvenience; I'm just curious about why it is necessary: > > PS> $(foreach($i in 1,2,3){$i}) | gm > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Why can't a foreach block begin a pipeline? OK. That's a pretty good reason. ![]() "George Xie [MSFT]" <gxie@online.microsoft.com> wrote in message news:%23KIe%23tF%23GHA.924@TK2MSFTNGP03.phx.gbl... > The grammar for pipeline statement is that it is made up of a list of > expression or command connected with pipes. Foreach, on the other hand, is > a statement. So the command below is eventually the same as following two > commands, > > foreach($i in 1,2,3){$i} > | gm > > The reason for this behaviour is purely a design decision so that clean > cut between different layers of language structure. > -- > George Xie [MSFT] > Microsoft Command Shell Development > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no > rights. > > "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message > news:%23W5sPUE%23GHA.4376@TK2MSFTNGP03.phx.gbl... >> Although some of the constraints on the internal foreach construct exist >> for understandable reasons, I can't see the reason that output generated >> within it is not treated as an output stream directly. Can anyone explain >> this? >> >> For example, the following causes an error: >> PS> foreach($i in 1,2,3){$i} | gm >> An empty pipe element is not permitted. >> At line:1 char:27 >> + foreach($i in 1,2,3){$i} | <<<< gm >> I could see this to overcome potential difficulties if you could move the >> enumerator backwards, but you can't. Even if you could, you could still >> forestall any problems with releasing objects by simply completing the >> block before emitting the output. >> >> The following _does_ work and takes minimal extra effort, so this isn't a >> major inconvenience; I'm just curious about why it is necessary: >> >> PS> $(foreach($i in 1,2,3){$i}) | gm >> > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Where to begin | General Discussion | |||
| foreach on a null interates the block? | PowerShell | |||
| foreach, foreach-object & begin/process/end scriptblock clauses... | PowerShell | |||
| Difference in semantics of for, foreach and foreach-object | PowerShell | |||
| possible bug with using the $foreach keyword inside foreach loops.. | PowerShell | |||