Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Next fucntion in loops?

Reply
 
Old 10-10-2006   #1 (permalink)
MKielman


 
 

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


 
 

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]


 
 

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


 
 

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


 
 

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
Reply

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


Vista Forums 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 Ltd

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