Looping through each element in an Array with N dimensions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jtung1027
    New Member
    • Sep 2007
    • 3

    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
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by jtung1027
    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

    [CODE=vb]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[/CODE]

    or if you dont know the dimension, you can use a For Each:

    [CODE=vb]dim v as variant
    dim Dou1 as double
    For each v in myarray
    dou1=dou1+v
    next[/CODE]

    Hope that helps

    Comment

    • jtung1027
      New Member
      • Sep 2007
      • 3

      #3
      Originally posted by kadghar
      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

      [CODE=vb]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[/CODE]

      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?

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by jtung1027
        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

        Comment

        • jtung1027
          New Member
          • Sep 2007
          • 3

          #5
          Originally posted by kadghar
          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...

          Comment

          • Robbie
            New Member
            • Mar 2007
            • 180

            #6
            Originally posted by jtung1027
            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:
            [code=vb]
            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((CurrentRep eat - 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.Langua ge = "VBScript"
            SControl.Execut eStatement ExecString


            End Sub
            [/code]

            EDIT: I put the code in the Form_Load() event, obviously you'll probably want to move it to elsewhere

            Comment

            Working...