Jump to content

Merge Arrays into one


bartekd
 Share

Recommended Posts

I am trying to put 2 2d arrays into one. There are some examples of my main array, and the ones I want to add to it. So essentially, I want to add all the arrays into one (My Main Array).

Do you have any ideas on how to do this?

My main Array

[0]| Count|List1|List2|List3|List4|

[1]| -1

[2]| 0

[3]| 1

[4]| 2

[5]| 3

[6]| 4

[7]| 5

[8]| 6

[9]| 7

[10]| 8

[11]| 9

[12]| 10

[13]| 11

Then I have indiviudal Arrays for each of the lists.

EG

List1

[0]|Count|Records

[1]|0|418

[2]|1|2695

[3]|2|1423

[4]|3|748

[5]|4|720

[6]|5|797

[7]|6|6518

[8]|7|977

[9]|8|160

[10]|9|13

[11]|10|4

[12]|11|1

List2

[0]|Count|Records

[1]|-1|2

[2]|0|520

[3]|1|2297

[4]|2|1623

[5]|3|6850

[6]|4|863

[7]|5|584

[8]|6|139

[9]|7|20

[10]|8|9

[11]|9|2

List3

[0]|Count|Records

[1]|0|451

[2]|1|1862

[4]|3|541

[5]|4|554

[6]|5|1811

[9]|8|1623

[10]|9|341

[11]|10|7

List4

[0]|Count|Records

[1]|0|387

[2]|1|1388

[3]|2|1432

[4]|3|1372

[5]|4|1831

[6]|5|992

[7]|6|59

[8]|7|9

Link to comment
Share on other sites

There have been 2D versions of _ArrayConcatenate() posted. I think I posted one a long time ago too. Some searching should turn it up.

;)

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

There have been 2D versions of _ArrayConcatenate() posted. I think I posted one a long time ago too. Some searching should turn it up.

;)

I am trying to use your _ArrayConcatenate() but I can't get it to work. I wrote a couple samples of my arrays, but I can't get it to work. Let me know if you see what is wrong.

What I am trying to accomplish is having data1 & data2 date put into AttemptsByList (Making sure that the left most column matches up.

#include <Array.au3>
Global $AttemptsByList, $data1, $data2 


Global  $AttemptsByList[13][5] = [["Count", "List1", "List2","List3","List4"],["-1"],["0"],["1"],["2"],["3"],["5"],["4"],["6"],["7"],["8"],["9"],["10"]]
Global $data1[12][2] = [["Count", "List1"],["-1","10"],["0","20"],["1"],["2","32"],["3",""],["5","12"],["4","455"],["6","655"],["8","544"],["9","855"],["10","3"]]
Global $data2[11][2] = [["Count", "List1"],["0","28"],["1"],["2","32"],["5","12"],["4","455"],["6","85"],["7","45"],["8","44"],["9","85"],["10","53"]]
;~ Global $data2[13][2] = [["Count", "List1"],["-1"],["0"],["1"],["2"],["3"],["5"],["4"],["6"],["7"],["8"],["9"],["10"]]


_ArrayDisplay($AttemptsByList)
_ArrayDisplay($data1)
_ArrayDisplay($data2)




; Concatenate 2D arrays
$sMsg = "Concatenate 2D arrays:" & @CRLF
$RET = __ArrayConcatenate($AttemptsByList, $data1)
$iErrSav = @error
$iExtSav = @extended
_ArrayDisplay($RET)


Func __ArrayConcatenate(ByRef $avArrayTarget, Const ByRef $avArraySource)
    If Not IsArray($avArrayTarget) Then Return SetError(1, 1, -1); $avArrayTarget is not an array
    If Not IsArray($avArraySource) Then Return SetError(1, 2, -1); $avArraySource is not an array
    
    Local $iUBoundTarget0 = UBound($avArrayTarget, 0), $iUBoundSource0 = UBound($avArraySource, 0)
    If $iUBoundTarget0 <> $iUBoundSource0 Then Return SetError(1, 3, -1); 1D/2D dimensionality did not match
    If $iUBoundTarget0 > 2 Then Return SetError(1, 4, -1); At least one array was 3D or more
    
    Local $iUBoundTarget1 = UBound($avArrayTarget, 1), $iUBoundSource1 = UBound($avArraySource, 1)
    
    Local $iNewSize = $iUBoundTarget1 + $iUBoundSource1
    If $iUBoundTarget0 = 1 Then
        ; 1D arrays
        ReDim $avArrayTarget[$iNewSize]
        For $i = 0 To $iUBoundSource1 - 1
            $avArrayTarget[$iUBoundTarget1 + $i] = $avArraySource[$i]
        Next
    Else
        ; 2D arrays
        Local $iUBoundTarget2 = UBound($avArrayTarget, 2), $iUBoundSource2 = UBound($avArraySource, 2)
        If $iUBoundSource2 > $iUBoundTarget2 Then Return SetError(1, 5, -1); 2D boundry of source too large for target
        ReDim $avArrayTarget[$iNewSize][$iUBoundTarget2]
        For $r = 0 To $iUBoundSource1 - 1
            For $c = 0 To $iUBoundSource2 - 1
                $avArrayTarget[$iUBoundTarget1 + $r][$c] = $avArraySource[$r][$c]
            Next
        Next
    EndIf
    
    Return $iNewSize - 1
EndFunc;==>__ArrayConcatenate
Link to comment
Share on other sites

That function uses ByRef. The target array contains the results, and $RET contains the new max index:

; Concatenate 2D arrays
$RET = __ArrayConcatenate($AttemptsByList, $data1)
$iErrSav = @error
$iExtSav = @extended
_ArrayDisplay($AttemptsByList, "RET = " & $RET & "; Error = " & $iErrSav & "; Ext = " & $iExtSav)

;)

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

Thanks, but I am trying to achieve something that looks like this.

Global $AttemptsByList[13][5] = [["Count", "List1", "List2","List3","List4"],["-1","10","0"],["0","20","28"],["1","0"],["2","32","32"],["3","",""],["4","455","455"],["5","455","85"],["6",655,85],["7",0,45],["8",544,44],["9",855,85],["10",3,53]]

Is that possible?

Link to comment
Share on other sites

Thanks all, but I ended up going the loop route. here is my snipet of code if anyone is interested. I looped it as well for each colum (List1, list2, list3 etc). Here is what I went with:

$Column = 1
        for $M = 1 to UBound($AttemptsByList)-1 ;goes through the main array, and does one row at a time.
                for $J = 1 to UBound($aData)-1 ;goes through the sql data, and outputs the data
                    if $AttemptsByList[$M][0] = $aData[$J][0] Then
                        $AttemptsByList[$M][$Column] = $aData[$J][1]
                    EndIf
                Next
        Next

Thanks again PSaltyDS ;)

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