For Each - Next with a 2 dimensional array

For Each - Next with a 2 dimensional array

Anonymous
Not applicable
247 Views
5 Replies
Message 1 of 6

For Each - Next with a 2 dimensional array

Anonymous
Not applicable
I have an array dimensioned (1 to 30, 1 to 3), and
I need to step through
the first dimension and look at one of the values in
the second dimension.
For example, in one situation, I would look at (1-30,1)
while in another
situation I would need to look at (1-30,2). Specifically, I
have a list of
30 items, with in effect a synonym (and sometimes two
synonyms), and at
different times I need to look up one based on another. The
data might look
something like this:
array (1,1) = "768": array (1,2) =
"1/64" & Chr(34) & "=1'": array (1,3) =
acVp1_64in_1ft
the last
being an AutoCAD constant that may or may not apply, as I am using
some
scales that AutoCAD has no constant for.
At times I need to step through
looking for "768" to get "1/64" & Chr(34)
&
"=1'"
:  or acVp1_64in_1ft, at other
times I am searching for "1/64" & Chr(34)
&
"=1'"
to get "768" .

At this point I am
doing this with an array, and would like to use For
Each - Next rather than a
counter. Then again, maybe there is a better data
structure for what I am
doing?
I had thought about using a collection, but I suspect I will
eventually want
to populate the data structure from a file or database, and
as far as I can
tell, a collection requires hard coding to define. Again I
may be wrong.

Any help is GREATLY
appreciated!

Gordon

PS Sorry for the HTML post, but this was one
occation where that bold really comes in
handy!
0 Likes
248 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
I've never tried it with multidimensional arrays but you can use a For
Each loop to iterate an array:

Dim var As Variant

For Each var In myArray

Next

--
http://www.acadx.com

"Gordon Price" wrote in
message news:A91456760727365FB4A9A97E5CA60974@in.WebX.maYIadrTaRb...
0 Likes
Message 3 of 6

Anonymous
Not applicable
Hi,

 

I'm not sure if the for each works on 2 dim arrays.
But another way around this is to create one new class "ArrayElement" which has
three properties, "768", "1/64" and "acVp1_....." Then create an array of
objects of the class type. Takes up a bit more memory though.

 

Cheers,

 Coen


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
"Gordon Price" <gordon.price(nospam)@albedoconsulting.com> wrote in
message
href="news:A91456760727365FB4A9A97E5CA60974@in.WebX.maYIadrTaRb">news:A91456760727365FB4A9A97E5CA...
...

I have an array dimensioned (1 to 30, 1 to 3),
and I need to step through
the first dimension and look at one of the
values in the second dimension.
For example, in one situation, I would look
at (1-30,1) while in another
situation I would need to look at (1-30,2).
Specifically, I have a list of
30 items, with in effect a synonym (and
sometimes two synonyms), and at
different times I need to look up one based
on another. The data might look
something like this:
array (1,1) =
"768": array (1,2) = "1/64" & Chr(34) & "=1'": array (1,3)
=
acVp1_64in_1ft
the last being an AutoCAD constant that may or may not
apply, as I am using
some scales that AutoCAD has no constant for.
At
times I need to step through looking for "768" to get
"1/64" & Chr(34) &
"=1'"
:  or
acVp1_64in_1ft, at other times I am searching for
"1/64" & Chr(34)
& "=1'"
to get
"768" .

At this point I am doing this with an array,
and would like to use For
Each - Next rather than a counter. Then again,
maybe there is a better data
structure for what I am doing?
I had
thought about using a collection, but I suspect I will eventually want
to
populate the data structure from a file or database, and as far as I
can
tell, a collection requires hard coding to define. Again I may be
wrong.

Any help is GREATLY appreciated!

Gordon

PS Sorry for the HTML post, but this was one
occation where that bold really comes in
handy!
0 Likes
Message 4 of 6

Anonymous
Not applicable
 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

Hi,

 

I'm not sure if the for each works on 2 dim
arrays. But another way around this is to create one new class "ArrayElement"
which has three properties, "768", "1/64" and "acVp1_....." Then create an
array of objects of the class type. Takes up a bit more memory though.

 

That was the route I ended up taking. You
do loose the For Each - Next option tho'. Oh well, as long as it
works!

 

Gordon
0 Likes
Message 5 of 6

Anonymous
Not applicable
If you use a collection object to store the object
you can do the for each....

Something like this:

 

dim c as new collection

 

c.additem <obj>

 

"Gordon Price" <gordon.price(nospam)@albedoconsulting.com> wrote in
message
href="news:24A6BBB30962CE2FB8D10C485CB7770A@in.WebX.maYIadrTaRb">news:24A6BBB30962CE2FB8D10C485CB77...
...


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
 


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">

Hi,

 

I'm not sure if the for each works on 2 dim
arrays. But another way around this is to create one new class
"ArrayElement" which has three properties, "768", "1/64" and "acVp1_....."
Then create an array of objects of the class type. Takes up a bit more
memory though.

 

That was the route I ended up taking. You
do loose the For Each - Next option tho'. Oh well, as long as it
works!

 


size=2>Gordon
0 Likes
Message 6 of 6

Anonymous
Not applicable
Instead of creating a multidimensional array, create
an array of arrays. Declare your array like that:

 

'Declare and initialize my array

Dim mat(1 To 30) As Variant
Dim tmp(1 To 3) As
String

Dim i as Integer

 

For i = 1 to 30

    mat(i) = tmp

Next

 

Then, access your elements like that:


Dim var As Variant
For Each var In
mat
    MsgBox var(3)
Next

Hope this helps.

 

Alain


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
"Gordon Price" <gordon.price(nospam)@albedoconsulting.com> wrote in
message
href="news:A91456760727365FB4A9A97E5CA60974@in.WebX.maYIadrTaRb">news:A91456760727365FB4A9A97E5CA...
...

I have an array dimensioned (1 to 30, 1 to 3),
and I need to step through
the first dimension and look at one of the
values in the second dimension.
For example, in one situation, I would look
at (1-30,1) while in another
situation I would need to look at (1-30,2).
Specifically, I have a list of
30 items, with in effect a synonym (and
sometimes two synonyms), and at
different times I need to look up one based
on another. The data might look
something like this:
array (1,1) =
"768": array (1,2) = "1/64" & Chr(34) & "=1'": array (1,3)
=
acVp1_64in_1ft
the last being an AutoCAD constant that may or may not
apply, as I am using
some scales that AutoCAD has no constant for.
At
times I need to step through looking for "768" to get
"1/64" & Chr(34) &
"=1'"
:  or
acVp1_64in_1ft, at other times I am searching for
"1/64" & Chr(34)
& "=1'"
to get
"768" .

At this point I am doing this with an array,
and would like to use For
Each - Next rather than a counter. Then again,
maybe there is a better data
structure for what I am doing?
I had
thought about using a collection, but I suspect I will eventually want
to
populate the data structure from a file or database, and as far as I
can
tell, a collection requires hard coding to define. Again I may be
wrong.

Any help is GREATLY appreciated!

Gordon

PS Sorry for the HTML post, but this was one
occation where that bold really comes in
handy!
0 Likes