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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I access an array of arrays? 1

Status
Not open for further replies.

chdavisjr

Programmer
Jan 24, 2002
85
US
I received this in an e-mail newsletter:
Code:
Dim x, y
y = array(1, 2, 3)
x = Array(y, array(3, 4, 5), array(6, 7, 8))

that mentions using an array of an arrays. However, they do not indicate how to access the elements.

My question is:
How do I access each element of the array x?
Nothing I have tried (x(0), x(1) etc.) works!

Thanks in advance,
Chalmers
 
A question for John,

And if I take this to the next level, and create a 3rd dimension with say z = array(x, array(7,8,9), array(10,11,12)), would I access the elements with a = z(0)(0)(0)?
 
Hello, Narizz28:
I found some more info in this thread that explains a little.
"2 Dimensional Array" thread329-756337
In response to your question,
Dim x, y, z
y=Array(1,2,3)
x=Array(y,Array(3,4,5), Array(6,7,8))

z=Array(x, Array(7,8,9), Array(10,11,12))

I can access these elements:
z(0)(0)(0)=1
z(0)(0)(1)=2
z(0)(0)(2)=3
z(0)(1)(0)=3
z(0)(1)(1)=4
z(0)(1)(2)=5
z(0)(2)(0)=6
z(0)(2)(1)=7
z(0)(2)(2)=8

Which are the elements of the array x.
But I cannot access the elements 7, 8, 9, 10, 11, 12, 13 no matter what I try.
Chalmers
 
You will get a type mismatch. You can access z(1)(0) and z(2)(0) however.
 
Thanks, TomThumbKP!
This is still confusing, on exactly how to access elements of this type of an array. I mean, how could one go about accessing all the elements without knowing how the array was created or how many arrays are within arrays??
Anyway ...

JohnYingling, here is what I get:
z(0)(0)(0)=1
z(0)(0)(1)=2
z(0)(0)(2)=3
z(0)(1)(0)=3
z(0)(1)(1)=4
z(0)(1)(2)=5
z(0)(2)(0)=6
z(0)(2)(1)=7
z(0)(2)(2)=8
------------
z(1)(0)=7
z(1)(1)=8
z(1)(2)=9
------------
z(2)(0)=10
z(2)(1)=11
z(2)(2)=12
 
That's why I prefer to build complex data structures using dictionaries.
 
Thanks, TomThumbKP.
Would you show an example on how to use dictionaries to build the complex array in the examples above and how to access the different elements?

For example:

Dim x, y, z
y = array(1, 2, 3)
x = Array(y, array(3, 4, 5), array(6, 7, 8))
z = array(x, array(7,8,9), array(10,11,12))

z(0)(0)(0)=1
z(0)(0)(1)=2
z(0)(0)(2)=3
z(0)(1)(0)=3
z(0)(1)(1)=4
z(0)(1)(2)=5
z(0)(2)(0)=6
z(0)(2)(1)=7
z(0)(2)(2)=8
------------
z(1)(0)=7
z(1)(1)=8
z(1)(2)=9
------------
z(2)(0)=10
z(2)(1)=11
z(2)(2)=12

Thanks,
Chalmers
 
Here is a sample of a complex data structure. I can explain it further if you need.
Code:
Set oDic = CreateObject("Scripting.Dictionary")
oDic.Add "String", "Test"
oDic.Add "Bool", True
oDic.Add "Int", 1
arrTest = Array("1", "2")
oDic.Add "Array", arrTest
Set oTemp = CreateObject("Scripting.Dictionary")
oTemp.Add "First", "One"
oTemp.Add "Second", "Two"
oDic.Add "Dictionary", oTemp
Set oTemp = Nothing

ShowDic oDic, "oDic"

Sub ShowDic(oD, strName)
	For Each strKey In oD.Keys()
		Select Case TypeName(oD.Item(strKey))
			Case "Integer" 
				WScript.Echo strName & "(" & strKey & ") => " & oD.Item(strKey)
			Case "String"
				WScript.Echo strName & "(" & strKey & ") => " & oD.Item(strKey)
			Case "Boolean"
				WScript.Echo strName & "(" & strKey & ") => " & oD.Item(strKey)
			Case "Variant()"
				ShowArray oD.Item(strKey), strKey
			Case "Dictionary"
				ShowDic od.Item(strKey), strKey
			Case Else
				WScript.Echo "UNKNOWN TYPE: " & TypeName(od.Item(strKey)) & " " & strName & "(" & strKey & ") => " & oD.Item(strKey)
		End Select
	Next
End Sub

Sub ShowArray(oA, strName)
	For i = 0 To UBound(oA)
		Select Case TypeName(oA(i))
			Case "Integer" 
				WScript.Echo strName & "(" & i & ") => " & oA(i)
			Case "String"
				WScript.Echo strName & "(" & i & ") => " & oA(i)
			Case "Boolean"
				WScript.Echo strName & "(" & i & ") => " & oA(i)
			Case "Variant()"
				ShowArray oA(i), strName & "(" & i & ")"
			Case "Dictionary"
				ShowDic oA(i), strName & "(" & i & ")"
			Case Else
				WScript.Echo "UNKNOWN TYPE: " & TypeName(od.Item(strKey)) & " " & strName & "(" & strKey & ") => " & oD.Item(strKey)
		End Select
	Next
End Sub
 
Thank you!
I believe you have given me enought to get started.
I've been looking for an excuse to use Dictionary objects!
[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top