Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multdimensional Arrays 3

Status
Not open for further replies.

GPM4663

Technical User
Aug 9, 2001
165
GB
Hi Everyone,
I'm using Multidimensional arrays for the first time and am having a little bit of trouble. I have a 2d array called myArray(50,2) which will store a date in the first "column" and a unique integer in the second "column" i.e.

Date1, 1
Date2, 6
Date3, 5
Date4, 3
Date5, 2

What i need to do is search the "2nd Column" of the array for X and get the corresponding date value so that i can compare the dates. I hope this makes some sense.

Any help would be greatly appreciated.

Many thanks

GPM
 
Something like (not tested);

Dim myarray(1 to 6, 1 to 2) as variant

myarray(1,1)=Date1: myarray(1,2)=1
'assign other values to myarray(2-6 and 1-2)

for i = 1 to ubound(myarray,1)
if myarry(i,2)= 6 then 'find date corresponding to 6
debug.print myarray(i,1)
end if
next
 
How about
Code:
For X = 0 to 50
   For Y = 0 to 2
      If MyArray(X,Y) = "Date searched for" Then
         Do Stuff
      End If
   Next Y
Next X

Everybody body is somebodys Nutter.
 
Why bother with a 2D array?

Simply use the element index as your "unique number", then there is no need to do any search, just go pull out the value.
 
Guys,
Thanks very much for the quick replies that has definitely cleared up a few things for me. Does looping through the array like that start to have any time limitations when you get into large records or large amounts of searches?

mintjulep,
What do you mean when you say "just go and pull out the value" using the unique element? Are you refering to using a .find command?

many thanks

GPM
 
I Think that GMP meant that you could use a single dimention array and use the array element as the 'unique integer'

so for instance:
MyArray(1) = Date
Instead of Date1, 1

you would then find the required date with:
MyArray("unique integer")

Everybody body is somebodys Nutter.
 
Your question has been well answered. Particularly in your specific case by using a 1-D array with the identifier being the index. In general, however, I don't think collections get enough respect. You can specify key/data pairs and then use the keys to retrieve the data.

As in:
VBA help said:
Syntax

object.Add item, key, before, after

The Add method syntax has the following object qualifier and named arguments:

Part Description
object Required. An object expression that evaluates to an object in the Applies To list.
item Required. An expression of any type that specifies the member to add to the collection.
key Optional. A unique string expression that specifies a key string that can be used, instead of a positional index, to access a member of the collection.

So if you have a general set of keys and values, where the keys might not be integers and even if they are they might not be contiguous, or even if they are they might not start at 1, collections can be a handy associative array

_________________
Bob Rashkin
 
You don't have to loop at all.

using your example data:

Date1, 1
Date2, 6
Date3, 5
Date4, 3
Date5, 2

MyArray(0)=""
MyArray(1)=date1
Myarray(2)=date5
Myarray(3)=date4
MyArray(4)=""
MyArray(5)=date3
MyArray(6)=date2

If you want the date associated with the unique integer 5:

SomeVariable = MyArray(5)

 
Thanks everyone for the response it's a great help. Should start me on my way!

thanks again,

GPM
 
Yeah, Collections!!!!

Although in this case, mintjulep's suggestion re: 1d array may be the simplest. It really depends on how that "unique"-ness is being used.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top