?
Solved

searching a bi-dim array

Posted on 2002-07-17
4
Medium Priority
?
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 800 total points
ID: 7159702
'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
ID: 7159708
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
ID: 7159885
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 143

Expert Comment

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

764 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