zone97

Single Array to 2D array

6 posts in this topic

#1 ·  Posted (edited)

I have read where people have helped with this in the past, but none match the situation I have.

I have a 4 part repeating array.

say $aRRAY = ["1","2","3","4","a1","a2","a3","a4", "b1","b2","b3","b4"]

and I want to separate it into 2D array every 4 elements, part of the issue is you have to count the number of elements as its constantly going to change, but will always be in sets of 4.

so it would end up like

1      2      3      4
a1   a2   a3   a4
b1   b2   b3   b4

Like that?

Edited by zone97

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Share this post


Link to post
Share on other sites



zone97,

Simple maths gives you the solution:

#include <Array.au3>

Global $aArray = ["1","2","3","4","a1","a2","a3","a4", "b1","b2","b3","b4"]

$iRows = Int(UBound($aArray) / 4)

Global $aNewArray[$iRows][4]

$iIndex = 0

For $i = 0 To $iRows - 1
    For $j = 0 To 3
        $aNewArray[$i][$j] = $aArray[$iIndex]
        $iIndex += 1
    Next
Next

_ArrayDisplay($aNewArray, "", Default, 8)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Perfect as always Melba.. God, I have got to get my head around this..

Edited by zone97

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Share this post


Link to post
Share on other sites

I remember a made a function for this need :

#Include <Array.au3> ; just for _ArrayDisplay

Local $aRRAY = ["1","2","3","4","a1","a2","a3","a4", "b1","b2","b3","b4"]
Local $a2DArray = _Array1DTo2D($aRRAY, 4)
_ArrayDisplay($a2DArray)

 

Share this post


Link to post
Share on other sites

Awesome Also.


 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Share this post


Link to post
Share on other sites

To help with your head,  here is a modified Melba23 example basically the same but without an incrementing index.

#include <Array.au3>

Global $aArray = ["1", "2", "3", "4", "a1", "a2", "a3", "a4", "b1", "b2", "b3", "b4"]
Global $iNumOfCols = 4
Global $iRows = Int(UBound($aArray) / $iNumOfCols)

Global $aNewArray[$iRows][$iNumOfCols]

For $i = 0 To $iRows - 1
    For $j = 0 To $iNumOfCols - 1
        $aNewArray[$i][$j] = $aArray[($i * $iNumOfCols) + $j]
    Next
Next

ConsoleWrite('A 1D array treated as a 4 column 2D array whose row and column indexes are [1][1] = "' & _
        _Array1DArrayTo2DArray($aArray, 4, 1, 1) & '"' & @CRLF) ; Returns:- "a2"

_ArrayDisplay($aNewArray)

; Returns an element of a 1D array with the row and column indexes of a 2D array.  (Without error catching routines for rubbish in.)
Func _Array1DArrayTo2DArray($1DArray, $iNumOfCols2D, $iRow_Ndx, $iCol_Ndx)
    Return $1DArray[($iNumOfCols2D * $iRow_Ndx) + $iCol_Ndx]
EndFunc   ;==>_Array1DArrayTo2DArray

A 2d array could be viewed as a divided up version of a 1D array.  (See the mental picture)

The 1D array is divided up by the number of columns required.
So that each full row of the 2D array contains the number of  columns required.

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