Sign in to follow this  
Followers 0
Tapes

2D Array into 1D Array

7 posts in this topic

Is there a way to put a combine the columns of a 2D array and put them into a 1D array. Or Vice versa?

Share this post


Link to post
Share on other sites



Show an example of what's in the 2D array, and how you want it to look in the 1D array, and we can have something to work with.


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

Tapes,

2D to 1D might be done like this

;
;
;


#include <array.au3>

local $2DArray[2][5] = [ _
                        [1,2,3,4,5], _
                        ['a','b','c','d','e'] _
                        ]

local $1Darray = _2Dto1DArray($2DArray)

_arraydisplay($1Darray)

func _2Dto1DArray($array)

    if ubound($array,0) <> 2 then return seterror(1)    ;   not a 2D array

    ; create target temp array

    local $aTMP[ubound($array,1) * ubound($array,2)]

    ; populate temp array

    local $offset = 0

    for $1 = 0 to ubound($array,1) - 1
        for $2 = 0 to ubound($array,2) - 1
            $aTMP[$offset] = $array[$1][$2]
            $offset += 1
        Next
    Next

    return $aTMP

endfunc

This will combine 2 to 10 1d arrays to 1 2D array

#include <array.au3>

local $a1[20] = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $a2[10] = [1,2,3,4,5,6,7,8,9,0]
local $a3[50] = ['','',3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $a4[5] = [1,2,3,4,5]
local $a5[7] = [1,2,3,4,5,6,7]

local $a10 = _array_combine($a1,$a2,$a3,$a4,$a5)

_arraydisplay($a10)

func _array_combine($arr1,$arr2,$arr3=0,$arr4=0,$arr5=0,$arr6=0,$arr7=0,$arr8=0,$arr9=0,$arr10=0)

    ;-----------------------------------------------------------------------------------------------------------------
    ; combine 2 to 10 array into 1 2D array
    ;-----------------------------------------------------------------------------------------------------------------

    ; ensure at least two arrays
    if not IsArray($arr1) or not IsArray($arr2) then return seterror(1,0,0)

    ; find # of array passed ($1 = # of arrays)
    for $1 = 1 to 10
        if not isarray(eval("arr" & $1)) then ExitLoop
    Next

    ; set # of cols variable to # of arrays in parameter
    local $num_cols = $1 - 1
    local $max_rows = 0

    ; set # elements to largest array passed
    for $1 = 1 to $num_cols
        if ubound(eval("arr" & $1)) - 1 > $max_rows then $max_rows = ubound(eval("arr" & $1))
    next

    ConsoleWrite('Formatting target array as ' & $num_cols & ' columns and ' & $max_rows & ' elements.' & @LF)

    ; define result array
    local $aTarget[$max_rows][$num_cols], $aTemp

    ; populate result array
    for $1 = 0 to $num_cols -1
        $aTemp = eval("arr" & $1+1)
        for $2 = 0 to ubound($aTemp) - 1
            $aTarget[$2][$1] = $aTemp[$2]
        Next
    next

    return $aTarget

endfunc

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Kylomas I tried you're first one and its close to what I want, but just to clear things up I'll give you an example.

For example, lets keep your 2D array you gave me.

local $2DArray[2][5] = [ _  [1,2,3,4,5], _  ['a','b','c','d','e'] _  ]
 

The displayed array when I use your code gives me Array1D = [1,2,3,4,5,a,b,c,d,e]

what I'm looking for though is for the array to give me Array1D  [1a,2b,3c,4d,5e.. and so on for more rows]

Any more ideas?

Thanks for the help so far though!

Share this post


Link to post
Share on other sites

Tapes,

That is why BrewmanNH asked for more definition.  I just happened to have some example code laying around.

Give us a "real" world example with ALL requirements for best results.

kylomas 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#include <Array.au3>
local $2DArray[2][5] = [ [1,2,3,4,5],  ['a','b','c','d','e'] ]
Local $1Darray[UBound($2DArray, Ubound($2DArray, 0)) ]
For $I = 0 to UBound($2DArray, Ubound($2DArray, 0)) - 1
    $1Darray[$I] = $2DArray[0][$I] & $2DArray[1][$I]
Next
_ArrayDisplay($1Darray)


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's another method.

;

#include <Array.au3>

Local $aArray[3][4] = [["a","b","","d"], ["0"," - ","3","4"], ["","x","y","z"]]
;_ArrayDisplay($aArray)

Local $aNewArray[UBound($aArray, 2)]

For $i = 0 To UBound($aArray, 2) -1
    For $j = 0 To UBound($aArray) -1
        $aNewArray[$i] &= $aArray[$j][$i]
    Next
Next
_ArrayDisplay($aNewArray)

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