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

Next fucntion in loops?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-10-2006   #1 (permalink)
MKielman
Guest


 

Next fucntion in loops?

Hi, I am trying to figure out if there is a next function or something
similar that will allow me force a loop to go to the next iteration.
Please advise.

Thanks,

My System SpecsSystem Spec
Old 10-10-2006   #2 (permalink)
Sung M Kim
Guest


 

Re: Next fucntion in loops?


MKielman wrote:
> Hi, I am trying to figure out if there is a next function or something
> similar that will allow me force a loop to go to the next iteration.
> Please advise.
>
> Thanks,


You can use "Continue" keyword
Here is an example

[^_^]PS[6]>for ($i = 0; $i -lt 10; $i++) { if (($i % 2) -eq 0) {
continue; } else { $i } }
1
3
5
7
9

My System SpecsSystem Spec
Old 10-11-2006   #3 (permalink)
Bruce Payette [MSFT]
Guest


 

Re: Next fucntion in loops?

On a related note, if you want the current loop body to do something with
the next element in the list, you can use the $foreach variable which
contains the loop enumerator. This lets you do things as shown in the
following where we format pairs of numbers together:

PS (9) > foreach ($i in 1..10) { [void] $foreach.MoveNext() ; "({0},{1})" -f
$i, $foreach.Current }
(1,2)
(3,4)
(5,6)
(7,8)
(9,10)

There is also something similar in the switch statement. When iterating
through a collection of elements, $switch is bound to the list enumerator
allowing the clauses to access the next element in the clause. This would be
really useful for arg cracking if we didn't have the param() statrment in
scripts :-)

PS (1) > $list = "-a", 12, "-b", 13,"-c"
PS (2) > $a = $b = 0
PS (3) > $c = $false
PS (4) > "new: a=$a b=$b c=$c"
new: a=0 b=0 c=False
PS (5) > switch ($list)
>> {
>> "-a" { $a = $([void] $switch.MoveNext(); $switch.Current) }
>> "-b" { $b = $([void] $switch.MoveNext(); $switch.Current) }
>> "-c" { $c = $true }
>> }
>>

PS (6) > "new: a=$a b=$b c=$c"
new: a=12 b=13 c=True


-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx


"Sung M Kim" <DontBotherMeWithSpam@gmail.com> wrote in message
news:1160516664.628059.88910@i42g2000cwa.googlegroups.com...
>
> MKielman wrote:
>> Hi, I am trying to figure out if there is a next function or something
>> similar that will allow me force a loop to go to the next iteration.
>> Please advise.
>>
>> Thanks,

>
> You can use "Continue" keyword
> Here is an example
>
> [^_^]PS[6]>for ($i = 0; $i -lt 10; $i++) { if (($i % 2) -eq 0) {
> continue; } else { $i } }
> 1
> 3
> 5
> 7
> 9
>



My System SpecsSystem Spec
Old 10-11-2006   #4 (permalink)
MKielman
Guest


 

Re: Next fucntion in loops?

Thanks for the response but I don't quite understand.

Here is an example of what I am trying to do:

foreach ($a in $b) {

do something...
if ($? -eq 0) {
$foreach.movenext()
}
do more stuff...
}

I guess while I am at it, in perl there is the unless statement, is
there something like that in powershell? Or can I not the if statement?



Bruce Payette [MSFT] wrote:
> On a related note, if you want the current loop body to do something with
> the next element in the list, you can use the $foreach variable which
> contains the loop enumerator. This lets you do things as shown in the
> following where we format pairs of numbers together:
>
> PS (9) > foreach ($i in 1..10) { [void] $foreach.MoveNext() ; "({0},{1})" -f
> $i, $foreach.Current }
> (1,2)
> (3,4)
> (5,6)
> (7,8)
> (9,10)
>
> There is also something similar in the switch statement. When iterating
> through a collection of elements, $switch is bound to the list enumerator
> allowing the clauses to access the next element in the clause. This would be
> really useful for arg cracking if we didn't have the param() statrment in
> scripts :-)
>
> PS (1) > $list = "-a", 12, "-b", 13,"-c"
> PS (2) > $a = $b = 0
> PS (3) > $c = $false
> PS (4) > "new: a=$a b=$b c=$c"
> new: a=0 b=0 c=False
> PS (5) > switch ($list)
>>> {
>>> "-a" { $a = $([void] $switch.MoveNext(); $switch.Current) }
>>> "-b" { $b = $([void] $switch.MoveNext(); $switch.Current) }
>>> "-c" { $c = $true }
>>> }
>>>

> PS (6) > "new: a=$a b=$b c=$c"
> new: a=12 b=13 c=True
>
>
> -bruce
>

My System SpecsSystem Spec
Old 10-11-2006   #5 (permalink)
Brandon Shell
Guest


 

Re: Next fucntion in loops?

This does what you want... although Im not sure this is what your asking for

Code:
------
$b = 1,2,3,4,5,6

foreach ($a in $b) {
Write-Host "Hello: in checking $a"
if ($a -eq 3) {
#script code here
write-Host "$a is ok" }
else{
# if failed check... Skip
continue}
}


"MKielman" <mkielman@newsgroups.nospam> wrote in message
news:OZJyIQY7GHA.1492@TK2MSFTNGP02.phx.gbl...
> Thanks for the response but I don't quite understand.
>
> Here is an example of what I am trying to do:
>
> foreach ($a in $b) {
>
> do something...
> if ($? -eq 0) {
> $foreach.movenext()
> }
> do more stuff...
> }
>
> I guess while I am at it, in perl there is the unless statement, is there
> something like that in powershell? Or can I not the if statement?
>
>
>
> Bruce Payette [MSFT] wrote:
>> On a related note, if you want the current loop body to do something with
>> the next element in the list, you can use the $foreach variable which
>> contains the loop enumerator. This lets you do things as shown in the
>> following where we format pairs of numbers together:
>>
>> PS (9) > foreach ($i in 1..10) { [void] $foreach.MoveNext() ;
>> "({0},{1})" -f $i, $foreach.Current }
>> (1,2)
>> (3,4)
>> (5,6)
>> (7,8)
>> (9,10)
>>
>> There is also something similar in the switch statement. When iterating
>> through a collection of elements, $switch is bound to the list enumerator
>> allowing the clauses to access the next element in the clause. This would
>> be really useful for arg cracking if we didn't have the param() statrment
>> in scripts :-)
>>
>> PS (1) > $list = "-a", 12, "-b", 13,"-c"
>> PS (2) > $a = $b = 0
>> PS (3) > $c = $false
>> PS (4) > "new: a=$a b=$b c=$c"
>> new: a=0 b=0 c=False
>> PS (5) > switch ($list)
>>>> {
>>>> "-a" { $a = $([void] $switch.MoveNext(); $switch.Current) }
>>>> "-b" { $b = $([void] $switch.MoveNext(); $switch.Current) }
>>>> "-c" { $c = $true }
>>>> }
>>>>

>> PS (6) > "new: a=$a b=$b c=$c"
>> new: a=12 b=13 c=True
>>
>>
>> -bruce
>>


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
ASIO in Fruity Loops on Vista Cope Vista music pictures video 0 06-11-2008 05:01 PM
Loops inside Cmdlets Shay Levi PowerShell 10 10-23-2007 12:26 PM
Task manager loops back? 1jay1 Vista General 2 06-10-2007 12:26 AM
Traps and Foreach loops lawndart@gmail.com PowerShell 4 02-14-2007 01:24 PM
Reboot loops during installation of Vista artF Vista installation & setup 2 06-29-2006 09:43 PM


Update your Vista Drivers Update Your Drivers Now!!

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