ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Enumerating a multi-dimensional array (https://www.excelbanter.com/excel-programming/276912-enumerating-multi-dimensional-array.html)

Robert Stober

Enumerating a multi-dimensional array
 
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



Alan Beban[_3_]

Enumerating a multi-dimensional array
 
Perhaps you could clarify what "enumerate the whole thing" means?

Alan Beban

Robert Stober wrote:
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




Michael Harris \(MVP\)

Enumerating a multi-dimensional array
 
Robert Stober wrote:
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/scr...s_overview.asp

System Administration Scripting Guide - samples scripts
http://www.microsoft.com/downloads/r...eleaseID=38942

WSH 5.6 documentation download
http://www.microsoft.com/downloads/d...displaylang=en


Robert Stober

Enumerating a multi-dimensional array
 
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
"Alan Beban" wrote in message
...
Perhaps you could clarify what "enumerate the whole thing" means?

Alan Beban

Robert Stober wrote:
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






Alan Beban[_3_]

Enumerating a multi-dimensional array
 
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



Robert Stober wrote:
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
"Alan Beban" wrote in message
...

Perhaps you could clarify what "enumerate the whole thing" means?

Alan Beban

Robert Stober wrote:

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







Robert Stober

Enumerating a multi-dimensional array
 
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

"Alan Beban" wrote in message
...
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



Robert Stober wrote:
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
"Alan Beban" wrote in message
...

Perhaps you could clarify what "enumerate the whole thing" means?

Alan Beban

Robert Stober wrote:

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









Alan Beban[_3_]

Enumerating a multi-dimensional array
 
Rather than looping, consider

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

Alan Beban

Robert Stober wrote:
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

"Alan Beban" wrote in message
...

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



Robert Stober wrote:

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
"Alan Beban" wrote in message
...


Perhaps you could clarify what "enumerate the whole thing" means?

Alan Beban

Robert Stober wrote:


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








Steve Fulton

Enumerating a multi-dimensional array
 
Robert Stober wrote:
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.

--
Steve

The believer is happy; the doubter is wise. -Hungarian proverb




All times are GMT +1. The time now is 01:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com