Jump to content

How to create 2D arrays from subarrays in rows


jmor
 Share

Recommended Posts

If I create a 2D array with:

Local $b[2][3] = [[1, 2, 3], [4, 5, 6]]

No problem

But it would simplify my work a lot if I could create a 2D array by specifying row subarrays:

Local $a0[3] = [1, 2, 3]
Local $a1[3] = [4, 5, 6]
Local $b[2][3] = [$a0, $a1]

Apparently, this doesn't work, I get the following error:

: ==> Missing subscript dimensions in "Dim" statement.:
Local $b[2][3] = [$a0, $a1]
Local $b[2][3] = [^ ERROR

Basically, I would like to use subarrays (arrays for the rows) to construct the 2D array, and then, be able to normally retrieve the individual elements with, for example:

consoleWrite('$b01: ' & $b[0][1]

I am sure there is a work around, or there is something I overlooked.

Thanks for the help.

Link to comment
Share on other sites

Just use a variable to capture the sub array for example:

Local $a0[3] = [1, 2, 3]
Local $a1[3] = [4, 5, 6]
Local $b[2] = [$a0, $a1]

;~ Get Array Items
Local $ba = $b[0]
ConsoleWrite($ba[0] & @CRLF & $ba[1] & @CRLF & $ba[2] & @CRLF & @CRLF)
$ba = $b[1]
ConsoleWrite($ba[0] & @CRLF & $ba[1] & @CRLF & $ba[2] & @CRLF & @CRLF)

;~ Or using a loop

Local $ab
For $i = 0 To UBound($b) - 1
    $ab = $b[$i]
    For $j = 0 To UBound($ab) - 1
        ConsoleWrite($ab[$j] & @CRLF)
    Next
    ConsoleWrite (@CRLF)
Next

 

Edited by Subz
Posted incorrect code.
Link to comment
Share on other sites

*as long as they have the same number of rows,

you can also flip those sideways and slap them together

#include<array.au3>

Local $a0[3] = [1, 2, 3]
Local $a1[3] = [4, 5, 6]

_ArrayTranspose($a0)
_ArrayTranspose($a1)
_ArrayConcatenate($a0 , $a1)

_ArrayDisplay($a0)

 

Edited by iamtheky
*caveat

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

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