473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Looping through each element in an Array with N dimensions

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 1718
kadghar
1,295 Expert 1GB
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
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
1,295 Expert 1GB
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
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
180 100+
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

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

Similar topics

2
by: Jarle Kaste | last post by:
Take a look at this code: var arr = new Array(); arr = 1; // element 0 arr = 2; // element 1 arr = 3; // element 2 Is it possible to loop through this array without knowing the string-id's?...
4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
4
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the...
17
by: Roshawn | last post by:
Hi, I have a small function that uses a For...Next loop. I am trying to place the results of the loop in a string array. My code is as follows: Dim i as Integer Dim str() as String For i =...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
1
by: delraydog | last post by:
I have an associative array that I need to loop through, allbills, however, each element in this array requires processing by the user and I need to capture the users actions on the element and...
4
by: nico3334 | last post by:
I'm having problems with my code concerning a mulitdimensional array. Basically, I'm trying to create an array (arrTotal) which contains all of the data from 2 arrays (arrOne & arrTwo). Basically,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.