Jump to content

Help 2 dim Array assignment


Burgaud
 Share

Recommended Posts

single dimension array

local $ar[4] = ["text",2,3,4]

2 dim array

local $arr[5][4]

$arr[0] = ["text",2,3,4] not working?

Error: expected a "=" operator in assignment statement.

 

Must I go the long way?

$arr[0][0]  =

$arr[0][1]  =

$arr[0][2]  =

 

 

 

Link to comment
Share on other sites

This is how I make it.

; Creating an arrays
Local $aMainArray = [1]
Local $aSubArray = [1, 2, 3, "text"]

ConsoleWrite("Current amount of item is $aMainArray is " & UBound($aMainArray) & @CRLF)

; Adding $aSubArray to $aMainArray
Local $iRet = _TableAdd($aMainArray, $aSubArray)

; Verify if successfully
If $iRet = -1 Then
    ConsoleWrite("Failed to add a new item into array." & @CRLF)
    Exit
EndIf

ConsoleWrite("Successfully added a new item into array. Index is " & $iRet & " , Total amount is " & UBound($aMainArray) & @CRLF)

; Example to access to added $aSubArray

Local $aSub = $aMainArray[1]

For $i = 0 To UBound($aSub) - 1
    Local $vItem = $aSub[$i]
    ConsoleWrite("Index is " & $i & ", item is " & $vItem & @CRLF)
Next

Func _TableAdd(ByRef $aTable, $vItem)

    ; Create an array if $aTable is not array.
    If Not IsArray($aTable) Then Dim $aTable[0]

    ; Get current items.count
    Local $iCount = UBound($aTable)

    ; Increase size of Array by 1.
    ReDim $aTable[$iCount + 1]

    ; Add a new var Item to array.
    $aTable[$iCount] = $vItem

    ; If error then set index to -1. No space in array.
    If @error Then Return -1

    ; Success, return index of new added item
    Return $iCount

EndFunc ;==> _TableAdd

 

Edited by Ascer
Link to comment
Share on other sites

Just realised what you were trying to achieve you could use _ArrayAdd (as SlackerAI mentioned abover) or _ArrayInsert for example:

#include <Array.au3>
local $ar[4] = ["text",2,3,4]
local $arr[5][4]
_ArrayAdd($arr, _ArrayToString($ar))
;~ Or
_ArrayInsert($arr, 0, _ArrayToString($ar))
_ArrayDisplay($arr)

Alternatively you could just add them using a function for example:

#include <Array.au3>
local $1dArray = ["text", 2, 3, 4]
local $2dArray[5][2]

_AddArray($2dArray, $1dArray, 0, 0)

_ArrayDisplay($2dArray)

Func _AddArray(ByRef $_2dArray, $_1dArray, $i2dIndex = 0, $i1dStart = 0)
    If UBound($_1dArray) > UBound($_2dArray, 2) Then ReDim $_2dArray[UBound($_2dArray)][UBound($_1dArray)]
    For $i = $i1dStart To UBound($_1dArray) - 1
        If $i1dStart = 0 Then
            $_2dArray[$i2dIndex][$i] = $_1dArray[$i]
            ContinueLoop
        EndIf
        $_2dArray[$i2dIndex][$i - 1] = $_1dArray[$i]
    Next
EndFunc

 

Link to comment
Share on other sites

#include<array.au3>
local $a1 = ['test' , 1 , 2 , 3]
local $arr[5][4] = [[$a1[0] , $a1[1] , $a1[2] , $a1[3]]]
_ArrayDisplay($arr)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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...