PDA

View Full Version : Iterating through an array, and omitting results



gscarter
07-21-2008, 02:35 AM
If i am iterating through an array, and performing a test at each iteration for example:



For Each customer in month1
If customer = Gary & version = 1
End if
Next customer

I want to do multiple iterations through the array, but omit the array elements that satisfied the first 'IF' statement. Can i do:



For Each customer in month1
If customer = Gary & version = 1
customer = ""
End if
Next customer

For Each customer in month1
If customer = Gary & version = 2
End if
Next Customer

Not the best example. Will the second 'FOR EACH' iterate through omitting the results that were picked up by 'customer = Gary' in the first loop. Hence not finding any results because Gary has already been counted in the first loop.

Say the customers may have multiple versions, but i only want to count them in the first foreach.

Im trying to get my head around it myself so if this makes no sense please let me know.

Gary

RichardSchollar
07-21-2008, 03:24 AM
Hi Gary

I must admit it is not completely clear to me what you are doing, but I think just using a nested If could be what you want (by the way, i think you wanted And rather than the ampersand in your original):


For Each customer in month1
If customer = Gary Then
If version = 1 Then
customer = ""
Elseif version = 2 Then
'do something else
End If
End if
Next customer

Richard

gscarter
07-21-2008, 04:53 AM
Hi Gary

I must admit it is not completely clear to me what you are doing, but I think just using a nested If could be what you want (by the way, i think you wanted And rather than the ampersand in your original):


For Each customer in month1
If customer = Gary Then
If version = 1 Then
customer = ""
Elseif version = 2 Then
'do something else
End If
End if
Next customer
Richard

Thanks, my point was can i use:



customer = ""

to clear an element in an 'customer' within my for each, for when i iterate through it again?

xld
07-21-2008, 05:00 AM
Why not combine them



For Each customer in month1
If customer = Gary And _
(version = 1 OR version = 2) Then

'do something
End if
Next Customer

gscarter
07-21-2008, 05:07 AM
Why not combine them



For Each customer in month1
If customer = Gary And _
(version = 1 OR version = 2) Then

'do something
End if
Next Customer

The idea behind what im doing is:

I have an array of unique customers.
I have a list in excel of customers and their versions.
A customer may be running two versions, hence they may appear in the list more than once.
I wish to iterate through the list using the array to find matches.

Starting with version 3, i want to iterate through each customer in the list to find a match with the first customer in the array, if a matching customer is found, and they are running version 3 i wish to eliminate that customer from the array, so it doesnt get counted when i iterate through for version 2, as the first customer could be running both, and i only want to count version 3.

Then iterate through the same list to find customers running version 2, but only if they havent already been picked up running version 3, if this makes sense.

So i wondered if i could just wipe that customer from the array using
customer = "" so when it iterated through the second time, even though that customer may well have been running version 2, it does not pick it up because it is erased from the array.

I hope this makes more sense.