Link to home
Start Free TrialLog in
Avatar of jhsd
jhsd

asked on

searching a bi-dim array

I have a bi-dim array say with 3 column and n rows.

I need to conduct a search in that array to see if there is a row which values match to 3 values I have say 'cc', 'ff', 'ddd' x each column

Thanks

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So something like:

Dim intRow As Integer
For intRow = LBound(aryMyArray(0)) To UBound(aryMyArray(0))
  If aryMyArray(0,intRow) = "cc" And aryMyArray(1,intRow) = "dd" And aryMyArray(2,intRow) = "ddd" Then
    Msgbox "Found a match at row " & CStr(intRow)
    Exit For
  End If
Next
Avatar of jhsd
jhsd

ASKER

thanx a lot also to TimCottee .
Kind angelll, I allow myself to profite of your knowledge to try to grab from u another quick answer :)
The thing is the following:
I load a di-dim array.
then when I go looping say:

Dim i As Integer
Dim h As Integer
'1 le righe

Dim str As String

For i = LBound(e, 1) To UBound(e, 1)
   For y = LBound(e, 2) To UBound(e, 2)
       
       str = str & "mystr" & (e(i, y)) & vbCrLf

   Next y
Next i
  MsgBox (str)


when I msgbox or debug.Print, str =

mystr*
mystr*
mystr*
mystr*
mystr*DE
mystr*01
mystr*cdr1
mystr*
mystr*DE
mystr*02
mystr*cdr1
mystr*
mystr*DE
mystr*01
mystr*cdr2
mystr*
mystr*DE
mystr*02
mystr*cdr2
mystr*

that is u can see there some void slots.

But if I do

For i = LBound(e, 1) To UBound(e, 1) -1
   For y = LBound(e, 2) To UBound(e, 2) -1
       
       str = str & "mystr" & (e(i, y)) & vbCrLf

   Next y
Next i

It seems I get right:

mystr*DE
mystr*01
mystr*cdr1
mystr*DE
mystr*02
mystr*cdr1
mystr*DE
mystr*01
mystr*cdr2
mystr*DE
mystr*02
mystr*cdr2

and this seems that what I wanted because I don't see 'bugs' in the slots.

My question is: I am doing ok, or by adding - 1 and -1
I am going to force something it is actually not right:
i.e.: there r some bug in the charging operation of the array.

Thanks x your precipous advive! Best Regards
I guess that the charging of the array has a bug in the loop.
As you start sizing the arrays with lower bound of 0, you must take care of this when increasing the size. I did the same error also in the beginning, now my loops to charge an array look like this:

dim lngArraySize as long
dim arrData()

lngArraySize = 0

'assume i read a recordset
while not rs.eof
  lngArraySize = lngArraySize + 1
  redim preserve arrData(1 to lngArraySize)
  arrData(lngArraySize) = rs.Fields("data").value
  rs.movenext
wend

What is important here is that the array is resized 1-based, and lngArraySize has the actual size of the array. So later on, when I want to loop the array, i do use lngArraySize instead of ubound(arrData), which is also much faster.

Now, to be honest, for tuning reasons (redim preserve is SLOW), i do even different:

dim lngArraySize as long
dim lngArrayMaxSize as long
dim arrData()

lngArraySize = 0
lngArrayMaxSize = 0

'assume i read a recordset
while not rs.eof
  lngArraySize = lngArraySize + 1
  if lngArraySize > lngArrayMaxSize
    lngArrayMaxSize = lngArrayMaxSize + 10
    redim preserve arrData(1 to lngArrayMaxSize)
  end if

  arrData(lngArraySize) = rs.Fields("data").value
  rs.movenext
wend

This means that the array is larger than it has values, but the loop is faster. The more important it is to keep the number of actual items in a variable for later processing.

CHeers