Jump to content

Single Array to 2D array


zone97
 Share

Recommended Posts

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 ]

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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)

 

Link to comment
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.

Link to comment
Share on other sites

  • 2 years later...
Quote

i not saw  it  thankz so much i like it , why   this  udf  is not introduction on core of autoit ???

It's not really a common enough use case to be legitimately entertained, but this question in @jguinch's thread made me explore if the result could be achieved given existing UDFs.  With no concern for being the optimal solution, just ones using UDFs and the fewest of them.

down to 3 right now:

#Include <Array.au3>

Local $sString =  "<select>" & @CRLF & _
                  "  <option value='volvo'>Volvo</option>" & @CRLF & _
                  "  <option value='saab'>Saab</option>" & @CRLF & _
                  "  <option value='mercedes'>Mercedes</option>" & @CRLF & _
                  "  <option value='audi'>Audi</option>" & @CRLF & _
                  "  <option value='porsche'>Porsche</option>" & @CRLF & _
                  "</select>"
Local $aValues = StringRegExp($sString, "value='([^']+)'>([^<]+)", 3)
_ArrayDisplay($aValues, "1D Array")

;~ $count = 2
$count = 5

local $aOut[0][$count]

For $i = 0 to ubound($aValues) - 1 step $count

$aX = _ArrayExtract($aValues , $i , $i + $count - 1)
_ArrayTranspose($aX)
_ArrayAdd($aOut , $aX)

Next

_ArrayDisplay($aOut)

 

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

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