Jump to content

Multi Dimension Array Question


Go to solution Solved by UEZ,

Recommended Posts

$var1 = "arg11|arg12|arg13|arg14 " & @CRLF & _
        "arg21|arg22|arg23|arg24 " & @CRLF & _
        "arg31|arg32|arg33|arg34 " & @CRLF & _
        "arg41|arg42|arg43|arg44 " & @CRLF & _
        "arg51|arg52|arg53|arg54 " & @CRLF

 

I would like to have array like this:

$Array[0][1] = "arg11"

$Array[0][2] = "arg12"

$Array[0][3] = "arg13"

$Array[0][4] = "arg14"

$Array[1][1] = "arg21"

$Array[1][2] = "arg22"

$Array[1][3] = "arg23"

$Array[1][4] = "arg24"

Using Function by wolf9228 found at '?do=embed' frameborder='0' data-embedContent>>

I'm able to do it, but I have to use _ArrayDelete to delete those empty elements returned by the function.

Is there any "better" way to do this without need to perform a blank element deletion after the function?

This is my current working code:

#include <Array.au3>

$var1 = "arg11|arg12|arg13|arg14 " & @CRLF & _
        "arg21|arg22|arg23|arg24 " & @CRLF & _
        "arg31|arg32|arg33|arg34 " & @CRLF & _
        "arg41|arg42|arg43|arg44 " & @CRLF & _
        "arg51|arg52|arg53|arg54 " & @CRLF

$Array = String_Split($var1, @CRLF, "|")

For $a = Ubound($Array)-1 To 0 Step -1
    If $Array[$a][0] = "" then _ArrayDelete($Array, $a)
Next

_ArrayDisplay($Array, "My Array")

; Func by wolf9228 at http://www.autoitscript.com/forum/topic/103115-split-array-from-2d-to-4d-solved/
Func String_Split($string ,$delimiters1 ,$delimiters2)
    $Array1 = StringSplit ( $string, $delimiters1)
    Dim $MAXUBound = 0 , $OutArray2[1][1]

    For $I = 1 To $Array1[0] Step 1
      $OutArray =  StringSplit ( $Array1[$i], $delimiters2)

      If $MAXUBound < $OutArray[0] Then
         ReDim $OutArray2[$i][$OutArray[0]]
         $MAXUBound = $OutArray[0]
      Else
         ReDim $OutArray2[$i][UBound($OutArray2,2)]
      EndIf

      For $j = 1 To $OutArray[0] Step 1
         $OutArray2[$i - 1][$j - 1] = $OutArray[$j]
      Next

    Next

    Return $OutArray2
EndFunc

 

Thank you, guys :)

Link to comment
Share on other sites

  • Solution

Try this:

 

#include <Array.au3>

$var1 = "arg11|arg12|arg13|arg14 " & @CRLF & _
        "arg21|arg22|arg23|arg24 " & @CRLF & _
        "arg31|arg32|arg33|arg34 " & @CRLF & _
        "arg41|arg42|arg43|arg44 " & @CRLF & _
        "arg51|arg52|arg53|arg54 " & @CRLF

$Array = StringSplitW($var1, "|")

_ArrayDisplay($Array, "My Array")

; #FUNCTION# ========================================================================================================================================
; Name .................:   StringSplitW()
; Description ..........:   Splits  a string into columns instead of rows as it is done by SplitString(), like a csv file to a 2d array ;-)
; Syntax ...............:   StringSplitW($sString, $sDelimiter, $iWidthLen)
; Parameters ...........:   $sString - string to split
;                           $sDelimiter - [optional] the delimter how to split the string
;                           $iWidthLen - [optional] length of the row (amount of columns - default is 256)
; Return values .......:    Success - 2d array
;                           Error 1 - either $sString or $delimter is not set
;                           Error 2 - array width exceeded
;                           Error 3 - error splitting string
;
; Version .............:    v0.96 build 2015-01-20 beta
; Author ..............:    UEZ
; Modified ............:
; Remarks .............:    RegEx take from http://stackoverflow.com/questions/4476812/regular-expressions-how-to-replace-a-character-within-quotes
; Related .............:    StringSplit, StringReplace, StringRegExpReplace, StringLen, StringStripCR
; ===================================================================================================================================================
Func StringSplitW($sString, $sDelimiter = ";", $sQuotationMark = '"', $sDummy = "¦", $iWidthLen = 256)
    If $sString = "" Or $sDelimiter = "" Then Return SetError(1, 0, 0)
    Local $chk, $iWidth, $i, $j, $k, $iLen, $iMax = 1, $iMaxWidth
    Local $aPos[1], $l = 0
    Local $aSplit =  StringSplit(StringStripCR($sString), @LF)
    If @error Then Return SetError(3, 0, 0)
    Local $aVertical[$aSplit[0]][$iWidthLen], $iDelimiterLen = StringLen($sDelimiter) - 1, $sLine
    For $k = 1 To $aSplit[0]
        $iLen = StringLen($aSplit[$k])
        If $iLen > 1 Then
            $sLine = StringRegExpReplace($aSplit[$k], '(?m)\' & $sDelimiter & '(?=[^' & $sQuotationMark & ']*' & $sQuotationMark & '(?:[^' & $sQuotationMark & '\r\n]*' & $sQuotationMark & '[^' & $sQuotationMark & ']*' & $sQuotationMark & ')*[^' & $sQuotationMark & '\r\n]*$)', $sDummy)
            $chk = StringReplace($sLine, $sDelimiter, $sDelimiter)
            $iWidth = @extended
            If $iWidth > $iWidthLen Then Return SetError(2, 0, 0)
            If $iWidth >= $iMax Then $iMax = $iWidth + 1
            Switch $iWidth
                Case 0
                    $aVertical[$l][0] = $sLine
                Case Else
                    Dim $aPos[$iWidth * 2 + 2]
                    $j = 1
                    $aPos[0] = 1
                    For $i = 0 To $iWidth - 1
                        $aPos[$j] = StringInStr($sLine, $sDelimiter, 0, $i + 1) - 1
                        $aPos[$j + 1] = $aPos[$j] + 2 + $iDelimiterLen
                        $j += 2
                    Next
                    $aPos[UBound($aPos) - 1] = StringLen($sLine)
                    $j = 0
                    For $i = 0 To UBound($aPos) - 1 Step 2
                        $aVertical[$l][$j] = StringMid(StringReplace($sLine, $sDummy, $sDelimiter), $aPos[$i], $aPos[$i + 1] - $aPos[$i] + 1)
                        $j += 1
                    Next
                EndSwitch
                $l += 1
        EndIf
    Next
    ReDim $aVertical[$l][$iMax]
    Return $aVertical
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Try this:

 

#include <Array.au3>

$var1 = "arg11|arg12|arg13|arg14 " & @CRLF & _
        "arg21|arg22|arg23|arg24 " & @CRLF & _
        "arg31|arg32|arg33|arg34 " & @CRLF & _
        "arg41|arg42|arg43|arg44 " & @CRLF & _
        "arg51|arg52|arg53|arg54 " & @CRLF

$Array = StringSplitW($var1, "|")

_ArrayDisplay($Array, "My Array")

; #FUNCTION# ========================================================================================================================================
; Name .................:   StringSplitW()
; Description ..........:   Splits  a string into columns instead of rows as it is done by SplitString(), like a csv file to a 2d array ;-)
; Syntax ...............:   StringSplitW($sString, $sDelimiter, $iWidthLen)
; Parameters ...........:   $sString - string to split
;                           $sDelimiter - [optional] the delimter how to split the string
;                           $iWidthLen - [optional] length of the row (amount of columns - default is 100)
; Return values .......:    Success - 2d array
;                           Error 1 - either $sString or $delimter is not set
;                           Error 2 - array width exceeded
;                           Error 3 - error splitting string
;
; Version .............:    v0.93 build 2013-08-23 beta
; Author ..............:    UEZ
; Modified ............:
; Remarks .............:
; Related .............:    StringSplit()
; ===================================================================================================================================================
Func StringSplitW($sString, $sDelimiter = ";", $iWidthLen = 256)
    If $sString = "" Or $sDelimiter = "" Then Return SetError(1, 0, 0)
    Local $chk, $iWidth, $i, $j, $k, $iLen, $iMax = 1, $iMaxWidth
    Local $aPos[1], $l = 0
    Local $aSplit =  StringSplit(StringStripCR($sString), @LF)
    If @error Then Return SetError(3, 0, 0)
    Local $aVertical[$aSplit[0]][$iWidthLen], $iDelimiterLen = StringLen($sDelimiter) - 1
    For $k = 1 To $aSplit[0]
        $iLen = StringLen($aSplit[$k])
        If $iLen > 1 Then
            $chk = StringReplace($aSplit[$k], $sDelimiter, $sDelimiter)
            $iWidth = @extended
            If $iWidth > $iWidthLen Then Return SetError(2, 0, 0)
            If $iWidth >= $iMax Then $iMax = $iWidth + 1
            Switch $iWidth
                Case 0
                    $aVertical[$l][0] = $aSplit[$k]
                Case Else
                    Dim $aPos[$iWidth * 2 + 2]
                    $j = 1
                    $aPos[0] = 1
                    For $i = 0 To $iWidth - 1
                        $aPos[$j] = StringInStr($aSplit[$k], $sDelimiter, 0, $i + 1) - 1
                        $aPos[$j + 1] = $aPos[$j] + 2 + $iDelimiterLen
                        $j += 2
                    Next
                    $aPos[UBound($aPos) - 1] = StringLen($aSplit[$k])
                    $j = 0
                    For $i = 0 To UBound($aPos) - 1 Step 2
                        $aVertical[$l][$j] = StringMid($aSplit[$k], $aPos[$i], $aPos[$i + 1] - $aPos[$i] + 1)
                        $j += 1
                    Next
                EndSwitch
                $l += 1
        EndIf
    Next
    ReDim $aVertical[$l][$iMax]
    Return $aVertical
EndFunc

Br,

UEZ

 

 

UEZ,

Works like a charm, thank you, you rock!! :thumbsup:

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