Solved

searching a bi-dim array

Posted on 2002-07-17
4
299 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 143

Accepted Solution

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating VB6 Function 19 65
MsgBox 2 59
Determine Range to Select 5 52
Set WorkSheet  not Working 9 38
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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