CodeLover
CodeLover

Reputation: 1074

Can we reset For loop counter in VBScript?

I have an Array say: VMHArray = (12,22,34,4) Now have another Arraylist object say ArrayListTaskDetails holding the data as (12,55,44,4,12,22,21,107,43,22,34) Now the below code I wrote to remove items from the list ArrayListTaskDetails which are not present in the VMHArray.

Code

Dim Flag : Flag = true
Dim Counter
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4

    Counter = 0
    Do while Flag

        If VMHArray(Counter) <> ArrayListTaskDetails (IndexSearch) Then

          ArrayListTaskDetails.RemoveRange IndexSearch, 4
          Flag = False

        End If
    Counter = Counter + 1
    Loop

Next

Now Suppose a match found at IndexSearch = 0,so according it would delete the element at locations 0,1,2,3 - quite good. But due to removal and to make the ArrayList object contiguous by functionality other elements will be shifted by 4 to left. Now the problem is as For Loop already incremented by 4,so it would start from next iteration from location 4. Thus now the new element at 0 location in the array list can never get a chance to test the equality with the VMHArray array elements. Can we handle such scenario without loosing the intended operation or anyway if we can set the For Loop counter to its immediate position,where the just recent match found and 4 elements removed.?

If any issue to understand the problem,let me know please!

EDIT Go to will not work

CODE(as per @Ankit suggestion)

Option Explicit

Dim Flag : Flag = true
Dim Counter
Dim VMHArray : VMHArray = Array(12,22,34,4) 
Dim ArrayListTaskDetails : Set ArrayListTaskDetails = CreateObject("System.Collections.ArrayList")

    ArrayListTaskDetails.Add(45)
    ArrayListTaskDetails.Add(4)
    ArrayListTaskDetails.Add(22)
    ArrayListTaskDetails.Add(4)
    ArrayListTaskDetails.Add(45)
    ArrayListTaskDetails.Add(20)
    ArrayListTaskDetails.Add(12)
    ArrayListTaskDetails.Add(35)
    ArrayListTaskDetails.Add(34)

    Restart:
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4

    Counter = 0
    Do while Flag

        If VMHArray(Counter) <> ArrayListTaskDetails (IndexSearch) Then

          ArrayListTaskDetails.RemoveRange IndexSearch, 4
          Goto Restart
          Flag = False

        End If
    Counter = Counter + 1
    Loop

Next

MsgBox(Join(ArrayListTaskDetails.ToArray()),"$")

Error

Thanks,

Upvotes: 1

Views: 9037

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Two ideas to solve the problem:

  1. use a dictionary to check for the items to keep
  2. loop backwards

In code:

Option Explicit

Dim alSrc : Set alSrc = CreateObject("System.Collections.ArrayList")
Dim sE
For Each sE In Split("a x x x b x x x c x x x d x x x e x x x f x x x a x x x")
    alSrc.Add sE
Next
WScript.Echo "alSrc:", Join(alSrc.ToArray(), ".")
Dim aKeep   : aKeep       = Split("a d")
WScript.Echo "aKeep:", Join(aKeep, ".")
Dim dicKeep : Set dicKeep = CreateObject("Scripting.Dictionary")
For Each sE In aKeep
    dicKeep(sE) = 0
Next
WScript.Echo "dicKeep:", Join(dicKeep.Keys(), ".")
Dim nI
For nI = alSrc.Count - 4 To 0 Step -4
    sE = alSrc(nI)
    If Not dicKeep.Exists(sE) Then
       alSrc.RemoveRange nI, 4
    End If
    WScript.Echo sE, nI, Join(alSrc.ToArray(), ".")
Next
WScript.Echo "alSrc:", Join(alSrc.ToArray(), ".")

output:

alSrc: a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.e.x.x.x.f.x.x.x.a.x.x.x
aKeep: a.d
dicKeep: a.d
a 24 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.e.x.x.x.f.x.x.x.a.x.x.x
f 20 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.e.x.x.x.a.x.x.x
e 16 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.a.x.x.x
d 12 a.x.x.x.b.x.x.x.c.x.x.x.d.x.x.x.a.x.x.x
c 8 a.x.x.x.b.x.x.x.d.x.x.x.a.x.x.x
b 4 a.x.x.x.d.x.x.x.a.x.x.x
a 0 a.x.x.x.d.x.x.x.a.x.x.x
alSrc: a.x.x.x.d.x.x.x.a.x.x.x

As to the mystery of .Count - ?:

1 2 3 4 5 6 7 8 - Count 1 - 8
0 1 2 3 4 5 6 7 - Idx   0 - 7
A x x x B x x x - Count (8) - Step (4) = Pos to check (4)
                  Count (8) - Step + 1 (5) = Not the pos to check (3)

Upvotes: 5

bonCodigo
bonCodigo

Reputation: 14361

Resetting For loop counter means you somewhat do not let it iterate. You have to be very careful not to run into an infinite loop. Frankly not reading your huge text. But looking at your code and title, please do try the following:

For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4
    Counter = 0
    Do while Flag
       If VMHArray(Counter) = ArrayListTaskDetails (IndexSearch) Then
          ArrayListTaskDetails.RemoveRange IndexSearch, 4
          Flag = False
          If IndexSearch >= 4 then 
            IndexSearch = IndexSearch - 4 '-- add this to go back to previous index
          End If
       End If
       Counter = Counter + 1
    Loop
Next IndexSearch

Upvotes: 0

Related Questions