By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Looping through each element in an Array with N dimensions

P: 3
Hi Fellas,

I am trying to write functions that are general enough to handle arrays of any size and dimension. Lets say I know the dimension size, and I've read somewhere that I should use a recursive for-loop function to loop through each dimension, but how will I access the elements of the array? The only way I know of is:

element = array( i , j , k , ... , N )

The argument of the array parameters doesn't accept anything but numbers so I can't generate a line of text that adds a comma and another number after each dimension iteration and then just plug in the string. It gives me wrong data type error. Maybe this is the wrong way to go about it. Thanks in advance.

Jay
Sep 5 '07 #1
Share this Question
Share on Google+
5 Replies


kadghar
Expert 100+
P: 1,295
Hi Fellas,

I am trying to write functions that are general enough to handle arrays of any size and dimension. Lets say I know the dimension size, and I've read somewhere that I should use a recursive for-loop function to loop through each dimension, but how will I access the elements of the array? The only way I know of is:

element = array( i , j , k , ... , N )

The argument of the array parameters doesn't accept anything but numbers so I can't generate a line of text that adds a comma and another number after each dimension iteration and then just plug in the string. It gives me wrong data type error. Maybe this is the wrong way to go about it. Thanks in advance.

Jay
what you want to do is to generate a string with the coordenates of the array??
Lets say, Str1

soy, you'll define Str1 = "1, 2 ,1"
and then get array(str1) ??

I dont think that'll work.

the common way to loop through each element with a FOR is something like

Expand|Select|Wrap|Line Numbers
  1. dim i as integer
  2. dim j as integer
  3. dim k as integer
  4. dim Dou1 as double
  5. for i = lbound(myarray,1) to ubound(myarray,1)
  6.     for j = lbound(myarray,2) to ubound(myarray,2)
  7.         for k = lbound(myarray,3) to ubound(myarray,3)
  8.             dou1 = dou1 + myarray(i,j,k)
  9.         next
  10.     next
  11. next
or if you dont know the dimension, you can use a For Each:

Expand|Select|Wrap|Line Numbers
  1. dim v as variant
  2. dim Dou1 as double
  3. For each v in myarray
  4.     dou1=dou1+v
  5. next
Hope that helps
Sep 5 '07 #2

P: 3
what you want to do is to generate a string with the coordenates of the array??
Lets say, Str1

soy, you'll define Str1 = "1, 2 ,1"
and then get array(str1) ??

I dont think that'll work.

the common way to loop through each element with a FOR is something like

Expand|Select|Wrap|Line Numbers
  1. dim i as integer
  2. dim j as integer
  3. dim k as integer
  4. dim Dou1 as double
  5. for i = lbound(myarray,1) to ubound(myarray,1)
  6.     for j = lbound(myarray,2) to ubound(myarray,2)
  7.         for k = lbound(myarray,3) to ubound(myarray,3)
  8.             dou1 = dou1 + myarray(i,j,k)
  9.         next
  10.     next
  11. next
Hope that helps
But what if the size of the array was variable? How can I loop through each element without manually changing the structure of the looping mechanism?
Sep 5 '07 #3

kadghar
Expert 100+
P: 1,295
But what if the size of the array was variable? How can I loop through each element without manually changing the structure of the looping mechanism?
sorry, i didnt wrote before the For Each... i forgot to copy - paste it, but then i edited the post

but you can use it

and you can also put some conditions on de variant, lets say you only want to count the numbers greater than 10

for each v in myarray
if v > 10 then dou1 = dou1 +1
next
Sep 5 '07 #4

P: 3
sorry, i didnt wrote before the For Each... i forgot to copy - paste it, but then i edited the post

but you can use it

and you can also put some conditions on de variant, lets say you only want to count the numbers greater than 10

for each v in myarray
if v > 10 then dou1 = dou1 +1
next
It would be great to be able to do something like this:

"For Each" goes through each object in a collection. But the structure is still the same as if I manually entered in the first object and the last object. My problem is how do I code a general statement to access the element of an N dimensioned array?

example:

N = 2 ; element = array( i , j)
N = 3 ; element = array( i , j , k)
N = 4 ; element = array( i , j , k , l)
etc...
Sep 5 '07 #5

Robbie
100+
P: 180
It would be great to be able to do something like this:

...

example:

N = 2 ; element = array( i , j)
N = 3 ; element = array( i , j , k)
N = 4 ; element = array( i , j , k , l)
etc...
This may seem like a long way round, but it should work.
You build up the code which you wish to execute (as a string), and then use a certain function to execute it.

Add a reference to Microsoft Script Control, then this should work for you:
Expand|Select|Wrap|Line Numbers
  1. Dim SControl As New ScriptControl
  2.  
  3. Private Sub Form_Load()
  4.  
  5.     Dim RepeatTimes As Integer
  6.     RepeatTimes = 5 'That's like the N in your example
  7.  
  8.     'Must be 1 or higher! (1 means that there would be only 'i')
  9.     'MUSTN'T BE HIGHER THAN 18!
  10.     '(Since we start at 'i', and 18 would be 'z')
  11.  
  12.     Dim ExecString As String
  13.  
  14. 'Start off the thing to execute:
  15.     ExecString = "element = Array(i"
  16.     Dim CurrentRepeat As Integer
  17. 'Now we go into a For...Next loop where we build up the ", j, k, l, m, n, "...etc, part:
  18.     For CurrentRepeat = 2 To RepeatTimes
  19.         'ASCII 'i' = character 105
  20.         ExecString = ExecString & ", " & Chr((CurrentRepeat - 1) + 105)
  21.     Next CurrentRepeat
  22.  
  23.     'Now end the 'element=Array' part with a close-bracket:
  24.     ExecString = ExecString & ")"
  25.  
  26.     'Now we need to execute that line.
  27.     'The line should look something like this:
  28.     element = Array(i, j, k, l, m)
  29.     '(If RepeatTimes is 5)
  30.  
  31.     'To execute it:
  32.     SControl.Language = "VBScript"
  33.     SControl.ExecuteStatement ExecString
  34.  
  35.  
  36. End Sub
  37.  
EDIT: I put the code in the Form_Load() event, obviously you'll probably want to move it to elsewhere
Sep 5 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.