Sign in to follow this  
Followers 0
Ronicus

Issues Using Arrays to Parse Data Help Needed Array Issue

6 posts in this topic

Ok so I'm attempting to parse data out of a txt file using a For loop and storing it in arrays I have this issue down the problem Im having is when I try to do Array1[$j] = $bodytext I don't understand how to get the body text fed into an array and make the array number numerically progress I want to be able to call an array to load that specific body text into my program Also is there a way to do something like Array[1] = Array[2] every time I try this the program loads than crashes out I hope I explained my issue well enough thanks for any help I have uploaded my code  keep in mind im trying to do multiple chapters and body texts I can figure it out if you could just give me an idea how to create an array that progresses

 

###
CHAPTER No: 1
###
##

title

##

#

bodytxt1

#

score.au3

Share this post


Link to post
Share on other sites



Sorry I didn't know it was acceptable to add this much code into a thread it will be several days before I can respond if you reply I live an hour from the nearest town with no internet

#include <WinAPIFiles.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <GuiComboBox.au3>

    Read ()
    Parsed()
    App()
    Func Read ()

    Global $rArray = FileReadToArray(@ScriptDir & "\Ptest.txt")
    Global $rBody
    Global $test
    Global $bCount = 0
    Global $tCount = 0
    Global $Title = ""
    Global $j = 0
    Global $k = 0
    Global $i = 0
    Global $cA
    Global $Body = ""
    Global $bState = 0
    Global $Chap
    Global $cList
    Global $cU

    If @error Then
            MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
        Else
            For $i = 0 To UBound($rArray) - 1

            If StringIsSpace($rArray[$i]) Then
                    $i = $i + "1"
                EndIf

                If $rArray[$i] = "##" Then
                    $tCount = $tCount + 1
                ElseIf $rArray[$i] = "#" Then
                    $bCount = $bCount + 1
                EndIf

                If $rArray[$i] = "###" Then
                    $i = $i + 1
                    $cA = $cA + 1
                    $cList = $cList & $rArray[$i] & "|"

                    $Chap = $rArray[$i]

                EndIf

                If $bCount = 1 Then
                    $i = $i + 1
                    $j = $i + 1
                    $bCount = $bCount + 2
                    $bState = 1
                ElseIf $bCount = 4 Then
                    $k = $i - 1
                    $bCount = $bCount + 1
                EndIf

                If $bCount = 5 Then
                    $bCount = 0
                EndIf

                If $bState = 1 Then
                    $Body = $Body & @CRLF & $rArray[$i]
                EndIf

                If $tCount = 1 Then
                    $i = $i + 1
                    $Title = $rArray[$i] & "    Free Islamic Education"
                EndIf

                If $tCount = 3 Then
                    $tCount = 1
                EndIf
        Next


        EndIf
    EndFunc

    Func Parsed()
                    MsgBox($MB_SYSTEMMODAL, "", "cA: " & $cA & @CRLF & "Chapter: " & $Chap & @CRLF & "Title: " & $Title & @CRLF & $Body & @CRLF & "Title Count: "& $tCount & @CRLF & "Body Count: " & $bCount & @CRLF & "Body Array: J " & $j & " K " & $k)

    EndFunc
Func App ()
            Opt("GUIOnEventMode", 1)
            Local $idEdit
            Local $1But
            local $2But
            Global $hwnd = GUICreate($Title, 800, 500)

            GUISetState(@SW_SHOW)


            GUICtrlCreateTab(10, 10, 790, 500)
            GUICtrlCreateTabItem("Select Test")
            $test = GUICtrlCreateCombo("", 20, 50, 150, 120)
            GUICtrlSetData(-1, $cList & "Select Chapter", "Select Chapter")
            $1But = GUICtrlCreateButton("Take Test", 180, 50, 80, 20)
            $2But = GUICtrlCreateButton("Exit", 180, 70, 80, 20)

            GUICtrlSetOnEvent($1But, "But1")
            GUICtrlSetOnEvent($2But, "But2")
            GUISetOnEvent($GUI_EVENT_CLOSE,"But2",$hwnd)


            GUICtrlCreateTabItem("Study")
            $idEdit = GUICtrlCreateEdit("", 12, 30, 790, 468)
            _GUICtrlEdit_SetText($idEdit, $Body)
            _GUICtrlEdit_SetReadOnly($idEdit, True)




            Do
            Until GUIGetMsg() = $GUI_EVENT_CLOSE
            GUIDelete()


EndFunc
            Func But1 ()

            Global $stext
            Global $dtext
            Global $sLength
            Global $tLength
            Global $sTrim

            $dtext =_GUICtrlComboBox_GetCurSel($test)
             _GUICtrlComboBox_GetLBText($test,$dtext,$stext)
            $sLength = StringLen($stext)
            $tLength = $sLength - 1
            $sTrim = StringTrimLeft($stext,$tLength)

            If $sTrim = "r" Then
                $sTrim = "No Selection Made"
            EndIf


            MsgBox($MB_SYSTEMMODAL, "Trimmed String ", "Trimmed: " & $sTrim & @CRLF & "String Length: " & $sLength)


            EndFunc


Func But2 ()
    GUIDelete($hwnd)
    Exit
    EndFunc

 

Share this post


Link to post
Share on other sites

Currently your code returns an error using the provided text
Could you post the complete initial txt file ?

Share this post


Link to post
Share on other sites

Thank you for responding so quickly here is the text from the file you see I want to read each body text out into an independent Array IE ARRAY[1] ARRAY[2] but supplicating the 1 and 2 for a variable of course so there is no limit the problem is I've only been able to figure out how to parse out both bodies into one variable and that variable isn't an array how do I make it an array every time it encounters a new # representing body text I want it to dump $body into an array then flush it out to collect the next chapters data

 

Quote
#include <WinAPIFiles.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <GuiComboBox.au3>

    Read ()
    Parsed()
    App()
    Func Read ()

    Global $rArray = FileReadToArray(@ScriptDir & "\Ptest.txt")
    Global $rBody
    Global $test
    Global $bCount = 0
    Global $tCount = 0
    Global $Title = ""
    Global $j = 0
    Global $k = 0
    Global $i = 0
    Global $cA
    Global $Body = ""
    Global $bState = 0
    Global $Chap
    Global $cList
    Global $cU

    If @error Then
            MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
        Else
            For $i = 0 To UBound($rArray) - 1

            If StringIsSpace($rArray[$i]) Then
                    $i = $i + "1"
                EndIf

                If $rArray[$i] = "##" Then
                    $tCount = $tCount + 1
                ElseIf $rArray[$i] = "#" Then
                    $bCount = $bCount + 1
                EndIf

                If $rArray[$i] = "###" Then
                    $i = $i + 1
                    $cA = $cA + 1
                    $cList = $cList & $rArray[$i] & "|"

                    $Chap = $rArray[$i]

                EndIf

                If $bCount = 1 Then
                    $i = $i + 1
                $bCount = $bCount + 2
                    $bState = 1
                ElseIf $bCount = 4 Then
                    $bCount = $bCount + 1
                    $bState = 0

                EndIf

                If $bCount = 5 Then
                    $bCount = 0

                EndIf

                If $bState = 1 Then
                    $Body = $Body & @CRLF & $rArray[$i]
                EndIf

                If $tCount = 1 Then
                    $i = $i + 1
                    $Title = $rArray[$i] & "    Free Islamic Education By Omar "
                EndIf

                If $tCount = 3 Then
                    $tCount = 1
                EndIf
        Next


        EndIf
    EndFunc

    Func Parsed()
                    MsgBox($MB_SYSTEMMODAL, "", "cA: " & $cA & @CRLF & "Chapter: " & $Chap & @CRLF & "Title: " & $Title & @CRLF & $Body & @CRLF & "Title Count: "& $tCount & @CRLF & "Body Count: " & $bCount & @CRLF & "Body Array: J " & $j & " K " & $k)

    EndFunc
Func App ()
            Opt("GUIOnEventMode", 1)
            Local $idEdit
            Local $1But
            local $2But
            Global $hwnd = GUICreate($Title, 800, 500)

            GUISetState(@SW_SHOW)


            GUICtrlCreateTab(10, 10, 790, 500)
            GUICtrlCreateTabItem("Select Test")
            $test = GUICtrlCreateCombo("", 20, 50, 150, 120)
            GUICtrlSetData(-1, $cList & "Select Chapter", "Select Chapter")
            $1But = GUICtrlCreateButton("Take Test", 180, 50, 80, 20)
            $2But = GUICtrlCreateButton("Exit", 180, 70, 80, 20)

            GUICtrlSetOnEvent($1But, "But1")
            GUICtrlSetOnEvent($2But, "But2")
            GUISetOnEvent($GUI_EVENT_CLOSE,"But2",$hwnd)


            GUICtrlCreateTabItem("Study")
            $idEdit = GUICtrlCreateEdit("", 12, 30, 790, 468)
            _GUICtrlEdit_SetText($idEdit, $Body)
            _GUICtrlEdit_SetReadOnly($idEdit, True)




            Do
            Until GUIGetMsg() = $GUI_EVENT_CLOSE
            GUIDelete()


EndFunc
            Func But1 ()

            Global $stext
            Global $dtext
            Global $sLength
            Global $tLength
            Global $sTrim

            $dtext =_GUICtrlComboBox_GetCurSel($test)
             _GUICtrlComboBox_GetLBText($test,$dtext,$stext)
            $sLength = StringLen($stext)
            $tLength = $sLength - 1
            $sTrim = StringTrimLeft($stext,$tLength)

            If $sTrim = "r" Then
                $sTrim = "No Selection Made"
            EndIf


            MsgBox($MB_SYSTEMMODAL, "Trimmed String ", "Trimmed: " & $sTrim & @CRLF & "String Length: " & $sLength)


            EndFunc


Func But2 ()
    GUIDelete($hwnd)
    Exit
    EndFunc
Quote

##
At- Tauhid (The Oneness of Allah)
##

###
CHAPTER No: 1

#
Body 1
#


###
CHAPTER No: 2

#
Body 2
#

 

 

Share this post


Link to post
Share on other sites

Im not sure how to edit the above post but I just fixed the flushing the $body issue so heres the new code now I just need to trigger $body to be read into a new Array every time

 

#include <WinAPIFiles.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <GuiComboBox.au3>

    Read ()
    Parsed()
    App()
    Func Read ()

    Global $rArray = FileReadToArray(@ScriptDir & "\Ptest.txt")
    Global $rBody
    Global $test
    Global $bCount = 0
    Global $tCount = 0
    Global $Title = ""
    Global $j = 0
    Global $k = 0
    Global $i = 0
    Global $cA
    Global $Body = ""
    Global $bState = 0
    Global $Chap
    Global $cList
    Global $cU

    If @error Then
            MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
        Else
            For $i = 0 To UBound($rArray) - 1

            If StringIsSpace($rArray[$i]) Then
                    $i = $i + "1"
                EndIf

                If $rArray[$i] = "##" Then
                    $tCount = $tCount + 1
                ElseIf $rArray[$i] = "#" Then
                    $bCount = $bCount + 1

                EndIf

                If $rArray[$i] = "###" Then
                    $i = $i + 1
                    $cA = $cA + 1
                    $cList = $cList & $rArray[$i] & "|"

                    $Chap = $rArray[$i]

                EndIf

                If $bCount = 1 Then
                    $i = $i + 1
                    $bCount = $bCount + 2
                    $bState = 1
                ElseIf $bCount = 4 Then
                    $bCount = $bCount + 1
                    $bState = 0
                ElseIf $bCount = 5 Then
                    $bState = 1
                    $bCount = 0
                    $Body = ""

                EndIf

                If $bState = 1 Then
                    $Body = $Body & @CRLF & $rArray[$i]
                EndIf

                If $tCount = 1 Then
                    $i = $i + 1
                    $Title = $rArray[$i] & "    Free Islamic Education By Omar "
                EndIf

                If $tCount = 3 Then
                    $tCount = 1
                EndIf
        Next


        EndIf
    EndFunc

    Func Parsed()
                    MsgBox($MB_SYSTEMMODAL, "", "cA: " & $cA & @CRLF & "Chapter: " & $Chap & @CRLF & "Title: " & $Title & @CRLF & $Body & @CRLF & "Title Count: "& $tCount & @CRLF & "Body Count: " & $bCount & @CRLF & "Body Array: J " & $j & " K " & $k)

    EndFunc
Func App ()
            Opt("GUIOnEventMode", 1)
            Local $idEdit
            Local $1But
            local $2But
            Global $hwnd = GUICreate($Title, 800, 500)

            GUISetState(@SW_SHOW)


            GUICtrlCreateTab(10, 10, 790, 500)
            GUICtrlCreateTabItem("Select Test")
            $test = GUICtrlCreateCombo("", 20, 50, 150, 120)
            GUICtrlSetData(-1, $cList & "Select Chapter", "Select Chapter")
            $1But = GUICtrlCreateButton("Take Test", 180, 50, 80, 20)
            $2But = GUICtrlCreateButton("Exit", 180, 70, 80, 20)

            GUICtrlSetOnEvent($1But, "But1")
            GUICtrlSetOnEvent($2But, "But2")
            GUISetOnEvent($GUI_EVENT_CLOSE,"But2",$hwnd)


            GUICtrlCreateTabItem("Study")
            $idEdit = GUICtrlCreateEdit("", 12, 30, 790, 468)
            _GUICtrlEdit_SetText($idEdit, $Body)
            _GUICtrlEdit_SetReadOnly($idEdit, True)




            Do
            Until GUIGetMsg() = $GUI_EVENT_CLOSE
            GUIDelete()


EndFunc
            Func But1 ()

            Global $stext
            Global $dtext
            Global $sLength
            Global $tLength
            Global $sTrim

            $dtext =_GUICtrlComboBox_GetCurSel($test)
             _GUICtrlComboBox_GetLBText($test,$dtext,$stext)
            $sLength = StringLen($stext)
            $tLength = $sLength - 1
            $sTrim = StringTrimLeft($stext,$tLength)

            If $sTrim = "r" Then
                $sTrim = "No Selection Made"
            EndIf


            MsgBox($MB_SYSTEMMODAL, "Trimmed String ", "Trimmed: " & $sTrim & @CRLF & "String Length: " & $sLength)


            EndFunc


Func But2 ()
    GUIDelete($hwnd)
    Exit
    EndFunc

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here is a quick way. It uses regular expressions but the same could be done using simpler (but much longer) ways
It works on the txt from post#4 . If you get some trouble then you might post the whole and complete  txt file as an attachment

The steps are explained in the comments  :)

#Include <Array.au3>

; read the txt file
$txt = FileRead(@ScriptDir & "\Ptest.txt")

; remove lines with # or empty
$txt = StringRegExpReplace($txt, '(?m)^[#\s]*\R?', "")
Msgbox(0,"text", $txt)

; get the title (first line)
$title = StringSplit($txt, @crlf, 3)[0]
Msgbox(0,"title", $title)

; get the chapters into an array
$chapters = StringRegExp($txt, '(?s)CHAPTER.*?(?=CHAPTER|$)', 3)
_ArrayDisplay($chapters, "chapters")

; then parse the chapters to get the bodies
$n = UBound($chapters)
Local $rArray[$n][2]
For $i = 0 to $n-1
    $rArray[$i][0] = StringSplit($chapters[$i], @crlf, 3)[0]
    $rArray[$i][1] = StringReplace($chapters[$i], $rArray[$i][0] & @crlf, "")
Next
_ArrayDisplay($rArray, $title)

 

Edited by mikell

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