Sign in to follow this  
Followers 0
strykerg

string to multidimensional array

4 posts in this topic

hi all,

i'm new here. my programming experience is all self-learnt so not much. as the title says im trying to divide a long string into a multidimensional array. i.e.:

aa (tab) aa1 (tab) aa2(@CRLF) bb (tab) bb1 (tab) bb2(@CRLF) cc (tab) cc1 (tab) cc2(@CRLF) dd (tab) dd1 (tab) dd2

into

$line[1] [1] = aa
$line[1] [2] = aa1
$line[1] [3] = aa2
$line[2] [1] = bb
$line[2] [2] = bb1
$line[2] [3] = bb2
$line[3] [1] = cc
$line[3] [2] = cc1
$line[3] [3] = cc2
$line[4] [1] = dd
$line[4] [2] = dd1
$line[4] [3] = dd2

 
Global $linelist
Global $linetabs
$thestring = "aa (tab) aa1 (tab) aa2(@CRLF) bb (tab) bb1 (tab) bb2(@CRLF) cc (tab) cc1 (tab) cc2(@CRLF) dd (tab) dd1 (tab) dd2"

    $linelist = StringSplit($thestring, "(@CRLF)",1)

    $i = 1
    While $i <= $linelist[0]
        $linetabs[$i] = StringSplit($linelist[$i], "(tab)",1)
        $i = $i + 1
    WEnd

    MsgBox(0, $i, $linelist[2]) ;ok this gives "bb (tab) bb1 (tab) bb2"
    MsgBox(0, $i, $linetabs[2][2]) ;shouldnt this give "bb1" ?? intead i get
    ;==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

can't understand what im doing wrong

thanks in advance!

Share this post


Link to post
Share on other sites



strykerg,

Welcome to the AutoIt forum. :)

You need to correctly structure the string and then learn a bit more about arrays. ;)

Take alook at this and see if you can follow:

#include <Array.au3>

Global $linelist

; A correctly structured string
$thestring = "aa" & @TAB & "aa1" & @TAB & "aa2" & @CRLF & "bb" & @TAB & "bb1" & @TAB & "bb2" & @CRLF & "cc" & @TAB & "cc1" & @TAB & "cc2" & @CRLF & "dd" & @TAB & "dd1" & @TAB & "dd2"

; Split on @CRLF
$linelist = StringSplit($thestring, @CRLF, 1)

; Check we actually have an array
If Not @error Then

    ; Display it
    _ArrayDisplay($linelist)

    ; Now check how many tabs there are in the line
    StringReplace($linelist[1], @TAB, "")
    $iCols = @extended

    ; And now create an array to hold all the elements when we split each line
    Global $linetabs[$linelist[0] + 1][$iCols + 1]

    ; Which we do here
    For $i = 1 To $linelist[0]
        ; Split the line into a temporary array
        $aTemp = StringSplit($linelist[$i], @TAB)
        ; And then move each element into the big array
        For $j = 1 To $aTemp[0]
            $linetabs[$i][$j - 1] = $aTemp[$j]
        Next
    Next

    ; And here is the result
    _ArrayDisplay($linetabs)

    ; And the elements are properly displayed
    MsgBox(0, $i, $linelist[2])
    MsgBox(0, $i, $linetabs[2][2])

EndIf

Please ask if you have any questions. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

damn that was fast.

yes I definitely need to learn stuff, and today you taught me quite a few :)

 _ArrayDisplay($linelist) << I love this! perfect debugging tool  :thumbsup:  

the only thing i'm not sure of is:

For $i = 1 To 50

do something

Next

is the same as

While ($i <= 50)

do something

$i = $i + 1

Wend

thanks a lot!

Share this post


Link to post
Share on other sites

strykerg,

Yes, the loops are functionally identical, but the For...Next version is more elegant - and you have less coding to do. ;)

m23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0