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
5 1718
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 - 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: - dim v as variant
-
dim Dou1 as double
-
For each v in myarray
-
dou1=dou1+v
-
next
Hope that helps
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 - 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?
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
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...
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: -
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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?...
|
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...
|
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...
|
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...
|
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 =...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |