Enumerating a multi-dimensional array

R

Robert Stober

Hi,

I've built a multi-dimensional array from a table and would like to
enumerate the whole thing, but I'm confused. Can anyone help me out?

Thank you,

Robert Stober
 
M

Michael Harris \(MVP\)

Robert said:
Hi,

I've built a multi-dimensional array from a table and would like to
enumerate the whole thing, but I'm confused. Can anyone help me out?


where arr is a 2-D array...

for j = 0 to ubound(arr,2)
for i = 0 to ubound(arr,1)
wscript.echo arr(i,j)
next
next


where arr is a 3-D array...

for k = 0 to ubound(arr,3)
for j = 0 to ubound(arr,2)
for i = 0 to ubound(arr,1)
wscript.echo arr(i,j,k)
next
next
next


--
Michael Harris
Microsoft.MVP.Scripting

Windows 2000 Scripting Guide
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

System Administration Scripting Guide - samples scripts
http://www.microsoft.com/downloads/release.asp?ReleaseID=38942

WSH 5.6 documentation download
http://www.microsoft.com/downloads/...48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en
 
R

Robert Stober

Alan,

I want to print out every defined value in the array. For instance if this
were a standard (one-deminsional) array, I could write:

Sub PrintArray()
Dim fruits As Variant
Dim i As Integer
fruits = Array("grapes", "pineapples", "kiwi")
For i = LBound(fruits) To UBound(fruits)
Debug.Print fruits(i)
Next i
End Sub

But my array isn't one deminsional, and I don't quite understand how my data
is being put into the array. There's a second argument to LBound and UBound
that I don't quite understand yet, so I want to print the contents of my
array so I can see how my data is organized.

Does that clear it up?

Thank you,

Robert
 
A

Alan Beban

Well, sorta'. You say it's multi-dimensional and not one-dimensional; is
it two-dimensional? Three-dimensional? Is it a secret?

If it's two-dimensional,

For i = LBound(fruits,1) to UBound(fruits,1)
For j=LBound(fruits,2) to UBound(fruits,2)
Debug.Print i,j,fruits(i,j)
Next j
Next i

If it's three-dimensional,

For i = LBound(fruits,1) to UBound(fruits,1)
For j=LBound(fruits,2) to UBound(fruits,2)
For k=LBound(fruits,3) to UBound(fruits,3)
Debug.Print i,j,k,fruits(i,j,k)
Next k
Next j
Next i

etc.

Alan Beban
 
R

Robert Stober

Alan,

Thank you. I also figured it out. Here's what I came up with (sorry about
the double spaces):

Dim j As Integer

Dim k As Integer

For j = LBound(locationNames, 1) To UBound(locationNames, 1)

For k = LBound(locationNames, 2) To UBound(locationNames, 2)

Sheets("Contents").Range("F3").Cells(j, k).Value = locationNames(j,
k)

Next k

Next j

Thank you for answering my question.

Robert
 
A

Alan Beban

Rather than looping, consider

Sheets("Contents").Range("F3").Resize(UBound(locationNames,1),_
UBound(locationNames,2)).Value = locationNames

Alan Beban
 
S

Steve Fulton

Robert said:
I've built a multi-dimensional array from a table and would like to
enumerate the whole thing, but I'm confused. Can anyone help me out?

For Each elm In ary
WScript.echo elm
Next

Apparently, VBScript stores arrays in column-major order, so this will
produce:

ary(0, 0, 0, ...)
ary(1, 0, 0, ...)
ary(2, 0, 0, ...)
...
ary(n, 1, 0, ...)
ary(n, 2, 0, ...)
...
etc.

But it's the simplest, most general way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top