![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Is there a 'continue' construct for loops in VBScript | VB Script | |||
| Vista loops on start up after avg is running | Vista General | |||
| ASIO in Fruity Loops on Vista | Vista music pictures video | |||
| Loops inside Cmdlets | PowerShell | |||
| Traps and Foreach loops | PowerShell | |||