conditional statement to compare records in array

# Thread: conditional statement to compare records in array

1. Join Date
Dec 1969
Posts
48

## conditional statement to compare records in array

Can anyone give me some type of direction towards an example of this topic. I have a multidimensional array that has 5 fields. I want to compare different records in two of those fields to satisfy a condition. I dont know how to do this. Do you know where to look for examples(what topic) or do you know how and can give me an example.

2. Junior Member
Join Date
Dec 1969
Posts
29

## RE: conditional statement to compare records in ar

Well two things. Can you just use a record structure instead of a multi-dimensional array? Might make things alot easier.<BR><BR>Second. Look at the code below:<BR><BR>&#060;%<BR><BR>&#039;Create a 2D array and a couple counters<BR>Dim arrFoods(3, 2)<BR>Dim i, j<BR><BR>&#039;Dump some sample information into 2D array<BR>arrFoods(1, 1) = "orange"<BR>arrFoods(1, 2) = "fruit"<BR>arrFoods(2, 1) = "cucumber"<BR>arrFoods(2, 2) = "vegetable"<BR>arrFoods(3, 1) = "pasta"<BR>arrFoods(3, 2) = "starch"<BR><BR>&#039;In order to navigate through a 2D array<BR>&#039;you need nested loops<BR>For i = 1 To 3<BR> For j = 1 To 2<BR> Response.Write arrFoods(i, j)<BR> Next<BR>Next<BR><BR>%&#062;<BR><BR>My take is that how ever many dimensions you&#039;re array is, is how many nested loops you&#039;ll need. Once you are in the &#039;inner-loop&#039; you can make all the comparisons you need to. Let me know if you understand how it gets all the data out of the array.

3. Join Date
Dec 1969
Posts
48

## RE: conditional statement to compare records in ar

Thanks for responding:<BR>The record structure method is not efficient enough for the task. It eats up too much time. <BR><BR>The code you showed me is exactly how I am looping through the array currently, but I need to be able to relate a field of one record to a field of another record and of that condition is true print the record.

4. Junior Member
Join Date
Dec 1969
Posts
29

## RE: conditional statement to compare records in ar

Not sure what you are trying to do. Lets look at the example again I added another record.<BR><BR>&#060;% <BR><BR>&#039;Create a 2D array and a couple counters <BR>Dim arrFoods(3, 2) <BR>Dim i, j <BR><BR>&#039;Dump some sample information into 2D array <BR>arrFoods(1, 1) = "orange" <BR>arrFoods(1, 2) = "fruit" <BR>arrFoods(2, 1) = "cucumber" <BR>arrFoods(2, 2) = "vegetable" <BR>arrFoods(3, 1) = "pasta" <BR>arrFoods(3, 2) = "starch"<BR>arrFoods(4, 1) = "tomato"<BR>arrFoods(4, 2) = "fruit" <BR><BR>&#039;In order to navigate through a 2D array <BR>&#039;you need nested loops <BR>For i = 1 To 3 <BR> For j = 1 To 2 <BR> Response.Write arrFoods(i, j) <BR> Next <BR>Next <BR><BR>%&#062; <BR><BR>Are you looking for a specific condition? If it has fruit in it for instance you want it to give you orange and tomato? <BR><BR>Or are you asking me how to compare 2 2D arrays to find what is in common between the two 2D arrays (I figured this one)?

5. Join Date
Dec 1969
Posts
48

## RE: conditional statement to compare records in ar

Yes, Im looking for a specific condition: <BR>This may help - I have a 2d array with 5 fields.<BR><BR>ID, Menutext, ParentID, SortOrder, Childcount<BR>-----------------------------------------<BR><BR>I reference these positions like this:<BR>comboArray(o, mycounter)<BR>comboArray(1, mycounter)<BR>comboArray(2, mycounter)<BR>comboArray(3, mycounter)<BR>comboArray(4, mycounter)<BR><BR>I want to write a function that gets passed a specific ID#. Then if that ID# is the same as any parentID#, Print the menuText of those records that match it. <BR><BR>I hope this is a lttle clearer. I know this is a confusing subject :)

6. Junior Member
Join Date
Dec 1969
Posts
29

## RE: conditional statement to compare records in ar

Ok what is up (had to goto lunch). Here is what I came up with<BR><BR>&#060;%<BR><BR>Private Function PassParam()<BR> Call Search2DArray("fruit")<BR>End Function<BR><BR>Private Function Search2DArray(strSearchCriteria As String)<BR><BR>Dim arrFoods(4, 2) As String<BR>Dim i As Integer, j As Integer<BR><BR>arrFoods(1, 1) = "orange"<BR>arrFoods(1, 2) = "fruit"<BR>arrFoods(2, 1) = "cucumber"<BR>arrFoods(2, 2) = "vegetable"<BR>arrFoods(3, 1) = "pasta"<BR>arrFoods(3, 2) = "starch"<BR>arrFoods(4, 1) = "tomato"<BR>arrFoods(4, 2) = "fruit"<BR><BR>For i = 1 To 4<BR> For j = 1 To 2<BR> &#039;Since you know which record you are to evaulate lock, that index<BR> &#039;for<BR> If strSearchCriteria = arrFoods(i, 2) Then<BR> Response.Write "Food: " & arrFoods(i, 1) & " Type: " & arrFoods(i, 2)<BR> Exit For<BR> End If<BR> Next<BR>Next<BR><BR>End Function<BR><BR>%&#062;

7. Junior Member
Join Date
Dec 1969
Posts
29

## RE: conditional statement to compare records in ar

Oops the output came out to be:<BR><BR>Food: orange Type: fruit<BR>Food: tomato Type: fruit<BR><BR>Is this what you want?<BR>

8. Join Date
Dec 1969
Posts
48

## RE: conditional statement to compare records in ar

Check out this code and tell me if you can relate it to what you showed me?<BR><BR><BR>traverse() <BR><BR>function traverse() <BR>&#039;This function goes through the array and prints each menu item with its children <BR><BR> dim myCounter <BR> myCounter = 0 <BR><BR> do <BR> response.write comboArray(1,myCounter) & " <BR>" <BR> printChildren comboArray(0,myCounter) <BR> myCounter = myCounter + 1 <BR> loop while myCounter &#060; iRecLast <BR><BR>end function <BR><BR>function printChildren(myParentID) <BR>&#039;This function get a parent ID and prints out its children <BR> <BR> IF myParentID = comboArray(2,i) Then <BR> Response.write "-" & comboArray(1,i) & " <BR>" <BR> end if <BR><BR>end function

9. Junior Member
Join Date
Dec 1969
Posts
29

## RE: conditional statement to compare records in ar

Ok look at your first loop it goes until iRecLast (say 5) its pattern then is to send 0,0; 0,1; 0,2; 0,3; 0,4; 0,5 to printChildren. then you are asking it to print matches where:<BR>0,0 = 2, i?...whats i? this may be part of your problem. i would suggest doing it in nested loops, if you understand them. it makes it easier. well here is another example. tell me if this is what you are looking to do. (in VBA)<BR><BR>Option Compare Database<BR>Option Explicit<BR><BR>Private Function PassParam()<BR> Call Search2DArray("file")<BR>End Function<BR><BR>Private Function Search2DArray(strSearchCriteria As String)<BR><BR>Dim arrFoods(4, 1) As String<BR>Dim intParent As Integer, intChild As Integer<BR><BR>&#039;Populate the 2D array with test values<BR>arrFoods(0, 0) = ""<BR>arrFoods(0, 1) = "file"<BR>arrFoods(1, 0) = "open"<BR>arrFoods(1, 1) = "file"<BR>arrFoods(2, 0) = "save"<BR>arrFoods(2, 1) = "file"<BR>arrFoods(3, 0) = "paste"<BR>arrFoods(3, 1) = "edit"<BR>arrFoods(4, 0) = "design"<BR>arrFoods(4, 1) = "view"<BR><BR>For intChild = 0 To 4<BR> For intParent = 0 To 1<BR> &#039;Since you know which record you are to evaulate, lock that index<BR> If strSearchCriteria = arrFoods(intChild, 1) Then<BR> If arrFoods(intChild, 0) = "" Then<BR> Debug.Print "Parent: " & arrFoods(intChild, 1)<BR> Else<BR> Debug.Print "Parent: " & arrFoods(intChild, 1) & " Child: " & arrFoods(intChild, 0)<BR> End If<BR> &#039;You have found a match at this point, not exiting will cause<BR> &#039;the match to duplicate<BR> Exit For<BR> End If<BR> Next<BR>Next<BR><BR>End Function

10. Junior Member
Join Date
Dec 1969
Posts
29

## RE: conditional statement to compare records in ar

Here is the output:<BR><BR>Parent: file<BR>Parent: file Child: open<BR>Parent: file Child: save

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•