435,204 Members | 1,212 Online + 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
5 Replies

 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 dim i as integer dim j as integer dim k as integer dim Dou1 as double for i = lbound(myarray,1) to ubound(myarray,1)     for j = lbound(myarray,2) to ubound(myarray,2)         for k = lbound(myarray,3) to ubound(myarray,3)             dou1 = dou1 + myarray(i,j,k)         next     next next or if you dont know the dimension, you can use a For Each: Expand|Select|Wrap|Line Numbers dim v as variant dim Dou1 as double For each v in myarray     dou1=dou1+v 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 dim i as integer dim j as integer dim k as integer dim Dou1 as double for i = lbound(myarray,1) to ubound(myarray,1)     for j = lbound(myarray,2) to ubound(myarray,2)         for k = lbound(myarray,3) to ubound(myarray,3)             dou1 = dou1 + myarray(i,j,k)         next     next 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

 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

 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 Dim SControl As New ScriptControl   Private Sub Form_Load()       Dim RepeatTimes As Integer     RepeatTimes = 5 'That's like the N in your example       'Must be 1 or higher! (1 means that there would be only 'i')     'MUSTN'T BE HIGHER THAN 18!     '(Since we start at 'i', and 18 would be 'z')       Dim ExecString As String   'Start off the thing to execute:     ExecString = "element = Array(i"     Dim CurrentRepeat As Integer 'Now we go into a For...Next loop where we build up the ", j, k, l, m, n, "...etc, part:     For CurrentRepeat = 2 To RepeatTimes         'ASCII 'i' = character 105         ExecString = ExecString & ", " & Chr((CurrentRepeat - 1) + 105)     Next CurrentRepeat       'Now end the 'element=Array' part with a close-bracket:     ExecString = ExecString & ")"       'Now we need to execute that line.     'The line should look something like this:     element = Array(i, j, k, l, m)     '(If RepeatTimes is 5)       'To execute it:     SControl.Language = "VBScript"     SControl.ExecuteStatement ExecString     End Sub   EDIT: I put the code in the Form_Load() event, obviously you'll probably want to move it to elsewhere Sep 5 '07 #6 