Jump to content

_Array1DTo2D


jguinch
 Share

Recommended Posts

We often have to get a 2D from a 1D array. A good example is with StringRegExp : it can only return a 1D array. But it can be interesting to transform the result to a 2D array.

Of course, it's easy to make it, but I think this function could help someone.

First, an example :

; #EXAMPLE# =====================================================================================================================
#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")

Local $aValues2D = _Array1DTo2D($aValues, 2, 0, 0, 0)
_ArrayDisplay($aValues2D, "2D Array")
; ===============================================================================================================================

Now, the function :

; #FUNCTION# ====================================================================================================================
; Name ..........: _Array1DTo2D
; Description ...: Transforms a 1D to a 2D array.
; Syntax ........: _Array1DTo2D($avArray, $iCols[, $iStart = 0[, $iEnd = 0[, $iFlag = 0]]])
; Parameters ....: $avArray             - Array to modify.
;                  $iCols               - Number of columns to transform the array to.
;                  $iStart              - [optional] Index of array to start the transformation. Default is the first element.
;                  $iEnd                - [optional] Index of array to stop the transformation. Default is the last element.
;                  $iFlag               - [optional] If set to 1, the array size must to a multiple of $iCols. Default is 0.
; Return values .: Success : Returns a 2D array
;                  Failure : Returns 0 and sets @error to :
;                    1 - $aArray is not an array
;                    2 - $iStart is greater than $iEnd
;                    3 - $aArray is not a 1D array
;                    4 - $aArray size is not a multiple of $iCols
; Author ........: jguinch
; ===============================================================================================================================
Func _Array1DTo2D($avArray, $iCols, $iStart = 0, $iEnd = 0, $iFlag = 0)

    If $iStart = Default OR $iStart < 0 Then $iStart = 0
    If $iEnd = Default Then $iEnd = 0

    If NOT IsArray($avArray) Then Return SetError(1, 0, 0)
    If UBound($avArray, 0) <> 1 Then Return SetError(3, 0, 0)

    Local $iUBound = UBound($avArray) - 1

    If $iEnd < 1 Then $iEnd = $iUBound
    If $iEnd > $iUBound Then $iEnd = $iUBound
    If $iStart > $iEnd Then Return SetError(2, 0, 0)

    Local $iNbRows = ($iEnd - $iStart + 1) / $iCols
    If $iFlag AND IsFloat($iNbRows) Then Return SetError(2, 0, 0)

    Local $aRet[ Ceiling($iNbRows) ][$iCols]
    Local $iCol = 0, $iRow = 0
    For $i = $iStart To $iEnd
        If $iCol = $iCols Then
            $iCol = 0
            $iRow += 1
        EndIf
        $aRet[$iRow][$iCol] = $avArray[$i]
        $iCol += 1
    Next

    Return $aRet
EndFunc

I hope it is clear enough

Edited by jguinch
Link to comment
Share on other sites

  • 5 years later...

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

×
×
  • Create New...