Announcement

Collapse
No announcement yet.

Skipping current iteration of a VBScript For Loop

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Skipping current iteration of a VBScript For Loop

    Is there a VBScript equivalent of "Continue" within a For Loop?

    I need to skip only 1 iteration.

  • #2
    Re: Skipping current iteration of a VBScript For Loop

    You can skip the current iteration based on a condition...

    Code:
    for i = 1 to 100
       if i = 9 then
          next
       end if 
    end for
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: Skipping current iteration of a VBScript For Loop

      Did you try that code before posting it?

      I copied and pasted it in to a new script but got an error:
      Code:
      Microsoft VBScript compilation error: Unexpected 'Next'
      I have in fact tried this technique before as you have written it but was greeted by the same error on those occasions too.

      Comment


      • #4
        Re: Skipping current iteration of a VBScript For Loop

        Are you just going to skip to the next value??

        Code:
        for i = 1 to 100
           if i = 9 then
              next
           end if 
        end for
        using Andrew's code change the if statement to this

        Code:
        for i = 1 to 100
           if i = 9 then
              i=10
           end if 
        end for

        Comment


        • #5
          Re: Skipping current iteration of a VBScript For Loop

          Originally posted by humbletech99 View Post
          Did you try that code before posting it?

          I copied and pasted it in to a new script but got an error:
          Code:
          Microsoft VBScript compilation error: Unexpected 'Next'
          I have in fact tried this technique before as you have written it but was greeted by the same error on those occasions too.
          The code I provided is pseudo in the syntax of VB.net. You do need to alter it a little to make it work for VBScript. This should work...

          Code:
          for i = 1 to 100
             if i = 9 then
                next
             end if
          next
          You can answer almost any syntactical or general reference question you might have by reading about VBScript here: http://msdn2.microsoft.com/en-us/library/d1wf56tt.aspx
          Last edited by ahinson; 17th December 2007, 04:10.
          Andrew

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

          Comment


          • #6
            Re: Skipping current iteration of a VBScript For Loop

            Originally posted by wullieb1 View Post
            Code:
            for i = 1 to 100
               if i = 9 then
                  i=10
               end if 
            end for
            Wouldn't this make you iterate on 10 twice?

            Also, I am not using i=0 to 10, but "for each in somecollection"


            ahinson: whenever I try that premature Next statement I always get the error I referred to you above with. I did reference msdn before posting and couldn't find the solution. I have in fact found out recently that not everything is 100% documented there...

            Comment


            • #7
              Re: Skipping current iteration of a VBScript For Loop

              In a For each loop normally you skip Items from a collection by name or object.value.
              The next example skips Items by its order of appearance;
              Code:
              iCnt = 1
              For each number in Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
                 If Not iCnt = 9 Then
                   wscript.echo number
                 End If
                 iCnt = iCnt + 1
              Next
              
              ''''''''''''''''''''''''''''''''''''''''' OR
              
              arrItems = Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
              For i = 0 To UBound(arrItems)
                 If Not 9 = i + 1 Then
                   wscript.echo arrItems(i)
                 End If
              Next
              Note that when the order of the Items in the collection (or in the array) changed, then you will have other results with this script.

              \Rems
              Last edited by Rems; 17th December 2007, 15:47.

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: Skipping current iteration of a VBScript For Loop

                wow that's weak. Thanks for trying to help me, I'm going to stick with the dictionary lookup I have working as this works more cleanly for me. thanks.

                Comment


                • #9
                  Re: Skipping current iteration of a VBScript For Loop

                  It's the nature of VBScript - it's limited. Use a real language to over come it. Then, use C++ or Delphi if you don't want to distribute the .net framework to deploy an application.
                  Andrew

                  ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

                  Comment


                  • #10
                    Re: Skipping current iteration of a VBScript For Loop

                    yes ok thank you, I think that I am already outgrowing vbs. I'll look at jscript first, and then perhaps move on to compiled languages properly later...

                    Comment


                    • #11
                      Re: Skipping current iteration of a VBScript For Loop

                      I think the closes alternative for "continue" in a For loop
                      would probably be "exit do" in a For:Do loop for usage in vbs.

                      syntax:
                      Code:
                        For each object in colObjects:Do
                           If object = exclusion _
                                Then Exit Do
                      
                        Exit Do:Loop : Next
                      just a (useless) example to get the idea:
                      Code:
                      Dim arr1 : arr1 = array(1,"one")
                      Dim arr2 : arr2 = array(2,"two")
                      Dim arr3 : arr3 = array(3,"three")
                      Dim arr4 : arr4 = "array(4, four)"   '<-the qoutes made this a string
                      Dim arr5 : arr5 = array(5,"five")
                      Dim arr6 : arr6 = array(6,"six")
                      Dim arr7 : arr7 = array(7,"seven")
                      Dim arr8 : arr8 = array(8,"eight")
                      Dim arr9 : arr9 = array(9,"nine")
                      Dim arr10 : arr10 = array(10,"ten")
                      Dim arr11 : arr11 = array(11,"eleven")
                      Dim arr12 : arr12 = array(12,"twelve")
                      Dim arr13 : arr13 = array(13,"thirteen")
                      Dim arr14 : arr14 = array(14,"fourteen")
                      Dim arr15 : arr15 = array(15,"fifteen")
                      
                      
                      Dim arrEvenArrays, EvenArray, i
                      arrEvenArrays = Array(arr2,arr4,arr6,arr8,arr10,arr12,arr14)
                       
                      For Each EvenArray In arrEvenArrays
                       Do While IsArray(EvenArray)
                      
                           If join(EvenArray) = join(arrEvenArrays((4)-1)) _
                              Then Exit Do
                      
                           For i = 0 To Ubound(EvenArray) Step 2
                      
                              If EvenArray(i) = arrEvenArrays(6-1)(1-1) _
                                 Then Exit Do
                      
                              wsh.echo "Even numbers between 1 and 15", vbNewLine, _
                                       "(four, eight and twelve were skipped by the For:Do Loop)", _
                                       vbNewLine, vbNewLine, space(4), EvenArray(i+1)
                      
                           Next
                      
                       Exit Do:Loop
                      Next
                      
                      
                      wscript.quit
                      ' http://www.visualbasicscript.com/m_43344/tm.htm
                      \Rems

                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      __________________

                      ** Remember to give credit where credit's due **
                      and leave Reputation Points for meaningful posts

                      Comment


                      • #12
                        Re: Skipping current iteration of a VBScript For Loop

                        hmm, embedding a second loop is a cunning workaround, thanks I'll keep that in mind!

                        Comment

                        Working...
                        X