Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to validate an array

Status
Not open for further replies.

celeron895

Programmer
Jan 29, 2002
81
US
Hello. I have an array consisting of boolean values. I need a validation script to tell if the array consists of only consecutive values. For example:

(0,0,1,1)
(1,1,0,0)
(1,1,1,1)
are all correct

whereas:
(0,1,0,1)
(0,0,1,0)
(1,1,0,1)
are not correct

 
Based on the examples, it seems that you're check consecutive pairs - that is the first and second elements need to be the same, and the third and fourth are the same, and so forth. With that in mind, you could try a loop something like the following:

IsOkay = True
for Idx = LBound(array) + 1 to UBound(array) step 2
if (array(Idx) <> array(Idx - 1)) then
IsOkay = False
End If
Next Idx




Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Actually, elements can ONLY appear conecutively. Further example:

(0,0,0,0,0,0)
(1,1,1,1,1,0)
(1,1,1,0,0,0)
are all good

(0,0,1,0,0,1) no good because there are 0's between the 1's
(1,1,1,1,0,1) no good because there are 0's between the 1's

It has nothing to do with pairs. Once the array has 'switched' to the oposite value, it cannont 'switch' back.
 
The modify the loop to check for the next element and the previous element. If an existing element is not the same as the one before it, AND not the same as the next one, then you have an invalid sequence. Note, that you'll don't check either the first or last element.

IsOkay = True
FrstSub = LBound(array)
LastSub = UBound(array)

For Idx = FirstSub + 1 to LastSub - 1
If ((array(Idx) <> array(Idx - 1)) AND (array(Idx) <> array(Idx + 1))) Then
IsOkay = False
End If
Next Idx
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top