Jump to content

Recommended Posts

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

 

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted (edited)

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

Edited by zone97

 

  Reveal hidden contents

 

Posted

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)

 

Posted

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.

  • 2 years later...
Posted
  Quote

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

Expand  

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)

 

  Reveal hidden contents

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...