Sign in to follow this  
Followers 0
LordBoling

Multidimensional Array Help

9 posts in this topic

Hey guys, I have been working on a program and have run into a bit of a snag.

Earlier in my script I created a multidimensional array of items from a webpage.

Global $oProd = _IETableWriteToArray($Table6,1)

This creates an array like this:

Row 0: <Qty Value> <Desc Value> <Mdl Value> <Tax Value> <Pr(ex) Value> <PR(inc) Value> <Ttl(ex) Value> <Ttl(inc) Value>

With additional rows for each consecutive item.

I now need a way for the user to add additional rows to the array. Any suggestions? Thanks.

Share this post


Link to post
Share on other sites



I'm not very good with arrays. But maybe this will get you on the right track. With it, you crate an array. Then if you want to add a row, you need to create a second array and copy the data from the first in to it. Then append the new row into the second array.

#include <Array.au3>

Local $avArray[3][3], $avArray2[4][3]

$avArray[0][0] = "Col1 Row1"
$avArray[1][0] = "Col1 Row2"
$avArray[2][0] = "Col1 Row3"

$avArray[0][1] = "Col2 Row1"
$avArray[1][1] = "Col2 Row2"
$avArray[2][1] = "Col2 Row3"

$avArray[0][2] = "Col3 Row1"
$avArray[1][2] = "Col3 Row2"
$avArray[2][2] = "Col3 Row3"


_ArrayDisplay($avArray, "Original Array")

$i = 0
$j = 0

Do
    $avArray2[$i][$j] = $avArray[$i][$j] ; Creates a second array and imports the data from the first.
        Do 
            $j = $j + 1
            $avArray2[$i][$j] = $avArray[$i][$j]
        Until $j = 2
    $i = $i + 1
    $j = 0
Until $i = 3

; Now begins the insert of a new row

$avArray2[3][0] = "Col1 Row4"
$avArray2[3][1] = "Col2 Row4"
$avArray2[3][2] = "Col3 Row4"   
    
_ArrayDisplay($avArray2, "Secondary Array")

#include <ByteMe.au3>

Share this post


Link to post
Share on other sites

Here's a modification to the _ArrayConcantenate that I made that works with 2 2D arrays that are have the same number of subdimensions. It might work for you if you already have a second array you want to add to the first.

; #FUNCTION# ====================================================================================================================
; Name...........: _ArrayConcatenate2D
; Description ...: Concatenate two arrays.
; Syntax.........: _ArrayConcatenate2D(ByRef $avArrayTarget, Const ByRef $avArraySource, $iStart = 0)
; Parameters ....: $avArrayTarget - The array to concatenate onto
;                  $avArraySource - The array to concatenate from
;                  $iStart - index of the first Source Array entry
; Return values .: Success - $avArrayTarget's new size
;                  Failure - 0, sets @error to:
;                  |1 - $avArrayTarget is not an array
;                  |2 - $avArraySource is not an array
;                  |3 - $avArrayTarget has different dimensions than $avArraySource
;                  |4 - $avArraySource is not a 2 dimensional array
;                  |5 - $iStart is an invalid index
; Author ........: Ultima
; Modified.......: Partypooper - added target start index, BrewManNH - modified to use 2D array
; Remarks .......:
; Related .......: _ArrayAdd, _ArrayPush
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ArrayConcatenate2D(ByRef $avArrayTarget, Const ByRef $avArraySource, $iStart = 0)
    If Not IsArray($avArrayTarget) Then Return SetError(1, 0, 0)
    If Not IsArray($avArraySource) Then Return SetError(2, 0, 0)
    If UBound($avArrayTarget, 2) <> UBound($avArraySource, 2) Then
        Return SetError(3, 0, 0)
    EndIf
    If UBound($avArraySource, 0) <> 2 Then Return SetError(4, 0, 0)
    Local $iUBoundTarget = UBound($avArrayTarget) - $iStart, $iUBoundTarget2 = UBound($avArrayTarget, 0), $iUBoundSource = UBound($avArraySource)
    If $iStart < 0 Or $iStart > $iUBoundSource - 1 Then Return SetError(5, 0, 0)
    ReDim $avArrayTarget[$iUBoundTarget + $iUBoundSource][$iUBoundTarget2]
    For $i = $iStart To $iUBoundSource - 1
        For $j = 0 To $iUBoundTarget2 - 1
            $avArrayTarget[$iUBoundTarget + $i][$j] = $avArraySource[$i][$j]
        Next
    Next
    Return $iUBoundTarget + $iUBoundSource
EndFunc   ;==>_ArrayConcatenate2D

On the other hand, if you're just looking to increase the number of rows in an array, look at the ReDim function.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Well, I can't seem to get these ideas to work still. I keep getting Array variable($oProd) has incorrect number of subscripts or subscript dimension range exceeded. Perhaps the array I am adding on here is bad. Here is what I have for adding the new item onto $oProd.

$array = $numProd
$numProd = $numProd + 1
ReDim $oProd[$numProd][8]
$new[0] = GUICtrlRead($InAddItemNum)
$new[1] = GUICtrlRead($InAddItemQty)
$new[2] = "N/A"
$new[7] = "N/A"
$oProd[$array] = $new

I also tried this:

$new[0] = GUICtrlRead($InAddItemNum)
$new[1] = GUICtrlRead($InAddItemQty)
$new[2] = "N/A"
$new[7] = "N/A" 
_ArrayConcatenate2D($oProd, $new)

When I use _ArrayConcatenate2D I do not get the error but when I use _ArrayDisplay it is the same array I started out with. Am I doing something wrong?

Share this post


Link to post
Share on other sites

You've changed to using a 1D array?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

You've changed to using a 1D array?

$oProd is a multidimensional array or should be. What did I do wrong? Thanks.

Share this post


Link to post
Share on other sites

$oProd may be multidimensional, but $new isn't it's a 1D array, and that UDF won't work with mismatched arrays. The 2 arrays have to have the same number of dimensions, and the same number of subitems in the 2nd dimension.

Your first try in the script in post #4 is trying to put the contents of the $new array in a single element of the array $oProd, but you're accessing it the wrong way, because $oProd is a 2D array, and you're trying to use it like it's a 1D array.

What you should be doing is something like this:

$numProd = $numProd + 1
ReDim $oProd[$numProd][8]
$new[0] = GUICtrlRead($InAddItemNum)
$new[1] = GUICtrlRead($InAddItemQty)
$new[2] = "N/A"
$new[7] = "N/A"
For $I = 0 to 7
    $oProd[$numprod - 1][$I] = $new[$I]
Next

You'll have to ReDim $oProd with one more row every time you go through this code, but it should do what you're trying to do.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Here is an example of inserting a 1D or 2D array (as an extra row or columns) into another 2D array.

#include <Array.au3>

Local $aArray[3][3] = [[1, 2, 3],[3, 4, 5],[5, 6, 7]]
_ArrayDisplay($aArray, "Original Array")


Local $aCol[3] = [7, 8, 9]
_ArrayInsert2D($aArray, "[7, 8, 9]", 2, 0) ; <-- Zero - Inserts column at 2nd column location.
;or
;_ArrayInsert2D($aArray, $aCol, 2, 0)
_ArrayDisplay($aArray, "Column Inserted @ Col 2")


Local $aRow[2][4] = [["a", "b", "c", "d"],["e", "f", "g", "h"]]
;_ArrayInsert2D($aArray, $aRow, 2, 1)
;or
_ArrayInsert2D($aArray, '[[a, "b", c, "d"],["e", f, "g", h]]', 2, 1) ; < -- One - Inserts rows at 2nd row location.
_ArrayDisplay($aArray, "Row Inserted @ Row [2]")



; Description: Enables a 1D or 2D array to be inserted (as a row[s] or column[s]) into a 2D array.
; Parameters:-
; $avArray - The array to have a row or column inserted.
; $iR_CLoc   - The location of the row or column number (base 0) where the row or column is to be inserted.
;                        If $iR_CLoc < 0 or -1 (default) then array $aInsert is appended to array $avArray.
; $aInsert - An array of the new row column or new column to be inserted into the array.
; $sRow      - If $sRow = 1 (default) then the $aInsert array will be inserted at the row $iR_CLoc value of the array, $avArray.
;                      If $sRow <> 1 then the $aInsert array will be inserted at the column $iR_CLoc value of the array, $avArray.

Func _ArrayInsert2D(ByRef $avArray, $aInsert, $iR_CLoc = -1, $sRow = 1)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0) ; Error: $avArray is not an array.
    If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, 0) ; Error: $avArray is not a 2 Dimensional array,

    ; Create 1D or 2D array from $aInsert string.
    If StringRegExp($aInsert, "^\[[^\[].*[^\]]\]$") Then $aInsert = StringSplit(StringTrimLeft(StringTrimRight($aInsert, 1), 1), ",", 3)
    If StringRegExp($aInsert, "^\[\[.*\]\]$") Then
        $aInsert = StringReplace($aInsert, "],[", ",")
        Local $iR = @extended + 1
        $aTemp1D = StringSplit(StringTrimLeft(StringTrimRight($aInsert, 2), 2), ",", 3)
        Local $aIns[$iR][UBound($aTemp1D) / $iR]
        $aInsert = $aIns
        For $i = 0 To $iR - 1
            For $j = 0 To UBound($aInsert, 2) - 1
                $aInsert[$i][$j] = $aTemp1D[$i * UBound($aInsert, 2) + $j]
            Next
        Next
    EndIf ; <-- End of Create 1D or 2D array from $aInsert string.

    If Not IsArray($aInsert) Then Return SetError(3, 0, 0) ; Error: $aInsert is not an array.

    If $sRow <> 1 Then ; Transpose array because of column insert.
        Dim $aTransArray[UBound($avArray, 2)][UBound($avArray, 1)]
        For $i = 0 To UBound($aArray, 1) - 1
            For $j = 0 To UBound($aArray, 2) - 1
                $aTransArray[$j][$i] = $aArray[$i][$j]
            Next
        Next
        $avArray = $aTransArray
    EndIf ; End of Transpose array if column insert.

    If (UBound($avArray, 2) = 1 And UBound($avArray, 2) <> UBound($aInsert, 1)) Or (UBound($avArray, 2) = 2 And _
            UBound($avArray, 2) <> UBound($aInsert, 2)) Then Return SetError(4, 0, 0); Error: Arrays, $avArray and $aInsert, do not have matching row or column lengths.
    If UBound($aInsert, 0) = 2 Then ; for 2D arrays
        If $iR_CLoc < 0 Or $iR_CLoc > UBound($avArray, 2) Then $iR_CLoc = UBound($avArray, 2)
        ; Add rows to the array
        Local $iUBound = UBound($avArray) + UBound($aInsert, 1)
        ReDim $avArray[$iUBound][UBound($avArray, 2)]
        For $i = $iUBound - 1 To $iR_CLoc + UBound($aInsert, 1) Step -1
            For $j = 0 To UBound($avArray, 2) - 1
                $avArray[$i][$j] = $avArray[$i - UBound($aInsert, 1)][$j]
            Next
        Next
        ; Add the rows' values in the specified elements.
        For $y = 0 To UBound($aInsert, 1) - 1
            For $x = 0 To UBound($aInsert, 2) - 1
                $avArray[$iR_CLoc + $y][$x] = $aInsert[$y][$x]
            Next
        Next
    Else ; for 1D arrays
        If $iR_CLoc < 0 Or $iR_CLoc > UBound($avArray, 1) Then $iR_CLoc = UBound($avArray, 1)
        ; Add 1 row to the array
        Local $iUBound = UBound($avArray) + 1
        ReDim $avArray[$iUBound][UBound($avArray, 2)]
        For $i = $iUBound - 1 To $iR_CLoc + 1 Step -1
            For $j = 0 To UBound($avArray, 2) - 1
                $avArray[$i][$j] = $avArray[$i - 1][$j]
            Next
        Next
        ; Add the row values in the specified elements.
        For $y = 0 To UBound($aInsert, 1) - 1
            $avArray[$iR_CLoc][$y] = $aInsert[$y]
        Next
    EndIf
    If $sRow <> 1 Then ; Transpose array because of column insert.
        Dim $aTransArray[UBound($avArray, 2)][UBound($avArray, 1)]
        For $i = 0 To UBound($aArray, 1) - 1
            For $j = 0 To UBound($aArray, 2) - 1
                $aTransArray[$j][$i] = $aArray[$i][$j]
            Next
        Next
        $avArray = $aTransArray
    EndIf ; End of Transpose array if column insert.
    Return 1
EndFunc   ;==>_ArrayInsert2D

Share this post


Link to post
Share on other sites

Thank you everyone. This information was very useful. This part of my script is working now and I will be able to use this in other places as well.

Share this post


Link to post
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
Sign in to follow this  
Followers 0