Solved

searching a bi-dim array

Posted on 2002-07-17
4
296 Views
Last Modified: 2010-05-02
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

0
Comment
Question by:jhsd
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
Comment Utility
'i assume these variable names
dim lngLoop as long
dim arrData() as variant

redim preserve arrData(1 to 3, x) as Variant

for lngLoop = lbound(arrData,1) to ubound (arrData,1)
 if arrData(1,lngloop) = "cc" _
    AND arrData(2,lngloop) = "ff" _
    AND arrData(3,lngloop) = "ddd" then
    msgbox lngLoop
 end if
next


CHeers
0
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
0
 

Author Comment

by:jhsd
Comment Utility
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
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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


0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now