Jump to content

Struggled with 2d array


Recommended Posts

Dear all,

I'm creating an import function from Excel to Autoit to a database.

now, from this excel document i use the ReadArray from ExcelCOM UDF, that all works fine.

See the array as following:

-------------------------------------------------

Field 1 | Field 2 | Field 3 | Field 4 | Field 5 |

-------------------------------------------------

Now i want to keep adding data to these fields, as being a table, for use later on BUT i don't know how long this array will be (for rows) so i cannot dim this max at front.

I've tried just adding it, within the use of my loopvariables, but it comes with the error: ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.

and that's obvious, there my array has the max rowlength as being [1][5]

:) I just tried to dim it as [999][5] at front (THIS WORKS :( ), but i'm not happy with this.

can someone please help me with a correct solution on this, i would really appreciate this.

Thanks in advance.

I also have the excel document added to this post, to get a feeling for the input.

This is my function:

func sp_com_read_column($oExcel)
    
    dim $a_Staffel[1][5]
    $a_Staffel[0][0] = "Staffelgrens"
    $a_Staffel[0][1] = "Tarief"
    $a_Staffel[0][2] = "Minimumbedrag"
    $a_Staffel[0][3] = "Bloktarief Aantal"
    $a_Staffel[0][4] = "Bloktarief Bedrag"
    
    
    $i =  2
    
    While 1
        $a_Column = _ExcelReadArray($oExcel, $i, 1, 5)
        
        if $a_Column[0] = "" Then
            ExitLoop
        EndIf
        
        for $y = 0 to Ubound($a_Column) -1
            
            msgbox(0, "", "$y = " & $i-1 & @CRLF & "$i = " & $y & @CRLF & "Value = " & $a_Column[$y])
            
        ; Begin filling the array with [1][0]
        ; Use begin by loopvariabels $i = 1, This is constant inside this loop, as it increments it's the loop thats the goal
        ;                            $y = 0, incremental inside this loop, the fiels as transposed vertically, needs to be used vertically

            $a_Staffel[$i-1][$y] = $a_Column[$y]
                
            _ArrayDisplay($a_Staffel)
        Next
        $i =  $i + 1
    WEnd
    _ArrayDisplay($a_Staffel, 0, 0, 1)
    
    
    
EndFunc
Link to comment
Share on other sites

You have to use the keyword ReDim, here's a little example I just made so you can see how it works :)

#include <array.au3>
Dim $array[1][5]
For $i=0 To 1000
    ReDim $array[UBound($array,1)+1][5]
    _ArrayDisplay($array)
    For $j=0 To 4
    $array[UBound($array,1)-1][$j]=$i
    Next
Next
_ArrayDisplay($array)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Thank you in advance!!!

you just made my day !!! :-)

have a nice weekend.

func sp_com_read_column($oExcel)
    
    dim $a_Staffel[1][5]
    $a_Staffel[0][0] = "Staffelgrens"
    $a_Staffel[0][1] = "Tarief"
    $a_Staffel[0][2] = "Minimumbedrag"
    $a_Staffel[0][3] = "Bloktarief Aantal"
    $a_Staffel[0][4] = "Bloktarief Bedrag"
    
    
    $i =  2
    
    While 1
        $a_Column = _ExcelReadArray($oExcel, $i, 1, 5)
        
        if $a_Column[0] = "" Then
            ExitLoop
        EndIf
        
        redim $a_Staffel[UBound($a_Staffel,1)+1][5]
        for $y = 0 to Ubound($a_Column) -1
            $a_Staffel[$i-1][$y] = $a_Column[$y]
                
        Next
        $i =  $i + 1
    WEnd
    
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...