Jump to content

MultiDim array


Go to solution Solved by Melba23,

Recommended Posts

Hello guys,

I need a little help for make a multidimensional array in this way:

I have a string delimited with "|" and i need to split between column, i have already make it:

abc abc abc

Now from every colum i need to split each character in this way

a a a

b b b

c c c

But if the character is between [ ] leave it in a single row, like:

a        a  a

b        b  [a12]

c        c   c

[a33]

d

f

Is clear enought? If not i'll try to explain better. My initial script:

#include <Array.au3> ; Only for _ArrayDisplay()

$sString = "testabc[12a]|test2589|test3578[34asd]|test4877"
StringReplace($sString, "|", "|")
$iCol = @extended

Global $aArray[1][$iCol+1]
$aCols = StringSplit($sString, "|", 1)
For $c = 1 To $aCols[0]
   $aArray[0][$c - 1] = $aCols[$c]
Next

_ArrayDisplay($aArray, "$aArray")
; until here is ok

For $x = 0 To UBound($aArray, 2) - 1
    ;StringSplit($aArray[0][$x], "", 1) ; split each char
Next

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

Johnmcloud,

Like this???

#include <Array.au3> ; Only for _ArrayDisplay()

$sString = "testabc[12a]|test2589|test3578[34asd]|test4877|abc[123]xyz"
StringReplace($sString, "|", "|")
$iCol = @extended

Global $aArray[1][$iCol+1]
$aCols = StringSplit($sString, "|", 1)
For $c = 1 To $aCols[0]
   $aArray[0][$c - 1] = $aCols[$c]
Next

;_ArrayDisplay($aArray, "$aArray")
; until here is ok

local $space = true, $sTmp
For $x = 0 To UBound($aArray, 2) - 1
    for $1 = 1 to stringlen($aArray[0][$x])
        if stringmid($aArray[0][$x],$1,1) = '[' then $space = not $space
        if not $space and stringmid($aArray[0][$x],$1,1) = ']' then $space = not $space
        $sTmp &= ($space) ? stringmid($aArray[0][$x],$1,1) & ' ' : stringmid($aArray[0][$x],$1,1)
    Next
    $aArray[0][$x] = $sTmp
    $sTmp = ''
Next

_ArrayDisplay($aArray)

It's 4:00AM and I could NOT work out the regexp for this so you get it the long winded way...

edit: corrected the for loop (should have started at 1

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

No ;)

Like this:

#include <Array.au3> ; Only for _ArrayDisplay()

$sString = "testabc[12a]|test2589|test3578[34asd]|abc[123]xyzuea"

Local $aArray[10][4]
$aArray[0][0] = "t"
$aArray[1][0] = "e"
$aArray[2][0] = "s"
$aArray[3][0] = "t"
$aArray[4][0] = "a"
$aArray[5][0] = "b"
$aArray[6][0] = "c"
$aArray[7][0] = "[12a]"

$aArray[0][1] = "t"
$aArray[1][1] = "e"
$aArray[2][1] = "s"
$aArray[3][1] = "t"
$aArray[4][1] = "2"
$aArray[5][1] = "5"
$aArray[6][1] = "8"
$aArray[7][1] = "9"

$aArray[0][2] = "t"
$aArray[1][2] = "e"
$aArray[2][2] = "s"
$aArray[3][2] = "t"
$aArray[4][2] = "3"
$aArray[5][2] = "5"
$aArray[6][2] = "7"
$aArray[7][2] = "8"
$aArray[7][2] = "[34asd]"

$aArray[0][3] = "a"
$aArray[1][3] = "b"
$aArray[2][3] = "c"
$aArray[3][3] = "[123]"
$aArray[4][3] = "x"
$aArray[5][3] = "y"
$aArray[6][3] = "z"
$aArray[7][3] = "u"
$aArray[8][3] = "e"
$aArray[9][3] = "a"
_ArrayDisplay($aArray)

Thanks anyway

Link to comment
Share on other sites

  • Moderators
  • Solution

johnmcloud,

This seems to work: :)

#include <Array.au3> ; Only for _ArrayDisplay()

$sString = "testabc[12a]|test2589|test3578[34asd]|abc[123]xyzuea"

Local $aArray[10][4]
$aArray[0][0] = "t"
$aArray[1][0] = "e"
$aArray[2][0] = "s"
$aArray[3][0] = "t"
$aArray[4][0] = "a"
$aArray[5][0] = "b"
$aArray[6][0] = "c"
$aArray[7][0] = "[12a]"

$aArray[0][1] = "t"
$aArray[1][1] = "e"
$aArray[2][1] = "s"
$aArray[3][1] = "t"
$aArray[4][1] = "2"
$aArray[5][1] = "5"
$aArray[6][1] = "8"
$aArray[7][1] = "9"

$aArray[0][2] = "t"
$aArray[1][2] = "e"
$aArray[2][2] = "s"
$aArray[3][2] = "t"
$aArray[4][2] = "3"
$aArray[5][2] = "5"
$aArray[6][2] = "7"
$aArray[7][2] = "8"
$aArray[7][2] = "[34asd]"

$aArray[0][3] = "a"
$aArray[1][3] = "b"
$aArray[2][3] = "c"
$aArray[3][3] = "[123]"
$aArray[4][3] = "x"
$aArray[5][3] = "y"
$aArray[6][3] = "z"
$aArray[7][3] = "u"
$aArray[8][3] = "e"
$aArray[9][3] = "a"
_ArrayDisplay($aArray, "Original", Default, 8)

; ###############################

; Split string on delimiter
$aSplit_1 = StringSplit($sString, "|")

; Create array with correct number of columns
Global $aFinalArray[1][$aSplit_1[0]]

; Loop through the split array
For $i = 1 To $aSplit_1[0]

    ; Extract line
    $sLine = $aSplit_1[$i]
    ; Get line length
    $iLen = StringLen($sLine)
    ; Create hold all chars if necessary
    Global $aSplit_2[$iLen + 1]
    ; Set indices
    $iWriteindex = 0
    For $iReadIndex = 1 To $iLen
        ; Get char
        $sChar = StringMid($sLine, $iReadIndex, 1)
        ; Add to array
        $aSplit_2[$iWriteindex] = $sChar
        ; if starting a [...] section
        If $sChar = "[" Then
            Do
                ; Keep looping through the line
                $iReadIndex += 1
                $sChar = StringMid($sLine, $iReadIndex, 1)
                ; Add all chars to the same element until end of [...] section
                $aSplit_2[$iWriteindex] &= $sChar
            Until $sChar = "]"
        EndIf
        ; Move to next element
        $iWriteindex += 1
    Next

    ; Just to see what happened
    ;_ArrayDisplay($aSplit_2, $sLine, Default, 8)

    ; ReDim final array if required
    If UBound($aFinalArray) < $iWriteindex Then
        ReDim $aFinalArray[$iWriteindex][$aSplit_1[0]]
    EndIf

    ; Add elements to final array
    For $j = 0 To $iWriteindex - 1
        $aFinalArray[$j][$i - 1] = $aSplit_2[$j]
    Next

Next

; And here is the result
_ArrayDisplay($aFinalArray, "M23", 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

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