Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 26
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 130
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 1
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 26
Default 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





  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 130
Default 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








  #6   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 26
Default 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








  #7   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 130
Default 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







  #8   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 1
Default 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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question on multi-dimensional array sammus New Users to Excel 2 April 3rd 06 03:11 AM
Problem with Multi-Dimensional Array Kirk[_2_] Excel Programming 2 August 26th 03 03:31 PM
How to declare Multi-dimensional dynamic array? Terence Excel Programming 1 August 11th 03 04:55 AM
Declaring Dynamic Multi-dimensional Array JohnV[_2_] Excel Programming 2 July 15th 03 06:58 PM
sort multi-dimensional array on numeric data? RB Smissaert Excel Programming 0 July 14th 03 10:49 PM


All times are GMT +1. The time now is 12:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"