Solved

searching a bi-dim array

Posted on 2002-07-17
4
297 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
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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

930 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

18 Experts available now in Live!

Get 1:1 Help Now