Jump to content

_ArrayAdd and _ArrayConcatenate in 2D arrays ?


tao
 Share

Recommended Posts

Is it possible?

I have a 2D array, such as:

$aArray[5][3] = [ _

[5, 20, 8], _

[4, 32, 7], _

[3, 16, 9], _

[2, 35, 0], _

[1, 19, 6]]

Then, say I have another 'record' i wish to tack on 'below it' , such as:

$bArray[1][3] = [[6, 9, 10]]

Is there an easy way to do this?

I've tried _ArrayAdd and _ArrayConcatenate to the best of my knowhow (I am still new to AutoIt)

Only other thing I can think of is to make sure $aArray is 'dim'ed big enough to allow me to run for loops in order to insert the values 1 at a time, but that seems really tedious. Any suggestions?

Ben

Link to comment
Share on other sites

Is it possible?

I have a 2D array, such as:

$aArray[5][3] = [ _

[5, 20, 8], _

[4, 32, 7], _

[3, 16, 9], _

[2, 35, 0], _

[1, 19, 6]]

Then, say I have another 'record' i wish to tack on 'below it' , such as:

$bArray[1][3] = [[6, 9, 10]]

Is there an easy way to do this?

I've tried _ArrayAdd and _ArrayConcatenate to the best of my knowhow (I am still new to AutoIt)

Only other thing I can think of is to make sure $aArray is 'dim'ed big enough to allow me to run for loops in order to insert the values 1 at a time, but that seems really tedious. Any suggestions?

Ben

This may need more testing for returning an accurate result in all situations. It appears to work.

I have tested by inserting $bArray[1][3] and $bArray[2][3] arrays at different locations into the main array.

#include <Array.au3>

Local $aArray[5][3] = [ _
[5, 20, 8], _
[4, 32, 7], _
[3, 16, 9], _
[2, 35, 0], _
[1, 19, 6]]

local $bArray[1][3] = [[6, 9, 10]]

_ArrayInsertArray($aArray, $bArray, 0)
_ArrayDisplay($aArray)

;Arrays are base 0.  To insert at beginning of $aMain array make $iIndex = 0
; $iIndex is the index of the $aMain array at which the $a2Insert array is inserted.
; $iIndex default -1 inserts or adds $a2Insert array to end of $aMain array
func _ArrayInsertArray(ByRef $aMain, ByRef $a2Insert, $iIndex = -1)
    if UBound($aMain,2) <> UBound($a2Insert,2) and UBound($aMain,0) <> UBound($a2Insert,0) Then return
    if $iIndex > UBound($aMain) or $iIndex < 0 then $iIndex = UBound($aMain)
    dim $atemp[UBound($aMain) + UBound($a2Insert)][UBound($aMain,2)]
    for $r = 0 to (UBound($aMain)) + (UBound($a2Insert) -1)
        for $c = 0 to UBound($aMain,2)-1
        if $r < $iIndex then $atemp[$r][$c] = $aMain[$r][$c]
        if $r = $iIndex then $atemp[$r][$c] = $a2Insert[0][$c]
        if $r > $iIndex and $r < ($iIndex + UBound($a2Insert))  then $atemp[$r][$c] = $a2Insert[$r-$iIndex][$c]         
        if $r > $iIndex and $r >= ($iIndex + UBound($a2Insert))  then $atemp[$r][$c] = $aMain[$r-UBound($a2Insert)][$c] 
        next
    next
    $aMain = $atemp
    return
EndFunc

Edit: Further testing with a 1x4 and a 3x4 arrays into a 6x4 array.

and tested inserting a 6x4 array into a 3x4 array. (Remember to _ArrayDisplay the correct array. That being the first array parameter in _ArrayInsertArray() function)

It all seems to be working ok

Edited by Malkey
Link to comment
Share on other sites

Is it possible?

I have a 2D array, such as:

$aArray[5][3] = [ _

[5, 20, 8], _

[4, 32, 7], _

[3, 16, 9], _

[2, 35, 0], _

[1, 19, 6]]

Then, say I have another 'record' i wish to tack on 'below it' , such as:

$bArray[1][3] = [[6, 9, 10]]

Is there an easy way to do this?

I've tried _ArrayAdd and _ArrayConcatenate to the best of my knowhow (I am still new to AutoIt)

Only other thing I can think of is to make sure $aArray is 'dim'ed big enough to allow me to run for loops in order to insert the values 1 at a time, but that seems really tedious. Any suggestions?

Ben

There is a 2D __ArrayConcatenate() UDF posted in another topic by some rakishly good-looking antarctic water fowl...

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...