Jump to content

Need to build an auto-replace text files


Recommended Posts

Hello everyone, I'm new in Autoit and I need to build an auto-replace text in files to replace a huge database test question from app to web and it looks like this:

File.ptx (or .txt)

1. What is the name of the cat?

*Doremon

Kitten

Cute

Dog

2.what is the name of the dog?

Doremon

Kitten

Cute

*Puppy

and output:

File .txt

Question: What is the name of the cat?

Choice: Doremon

Choice: Kitten

Choice: Cute

Choice: Dog

Correct:1

Question: What is the name of the dog?

Choice: Doremon

Choice: Kitten

Choice: Cute

Choice: Puppy

Correct:4

With "1." and "2." is number of questions,Correct is number which is right in line:

Line1: Question:

Line2: Choice:

Line3: Choice:

Line4: Choice:

Line5: Choice:

Line6: Correct: $line_number (in $line has *)

Sorry for my bad english. Many Tks for help!

$Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf
Link to comment
Share on other sites

  • Moderators

DucViet,

Something like this:

#include <File.au3>

#include <Array.au3> ; Just for display

; Read in file
Global $aLines
_FileReadToArray("Test.txt", $aLines)

; Declare result array
Global $aNewLines[1] = [0]

For $i = 1 To $aLines[0]
    ; Look for a question
    If StringRegExp($aLines[$i], "^\d+\.") Then
        ; Look for the answer
        For $j = 1 To 4
            If StringLeft($aLines[$i + $j], 1) = "*" Then
                ; Found it - remove leading *
                $aLines[$i + $j] = StringTrimLeft($aLines[$i + $j], 1)
                ; And note place
                $iAnswer = $j
                ExitLoop
            EndIf
        Next
        ; Where to insert this section
        $iIndex = $aNewLines[0] + 1
        ; ReDim teh array
        $aNewLines[0] += 7
        ReDim $aNewLines[$aNewLines[0] + 1]
        ; Insert the existing lines
        For $j = 0 To 4
            $aNewLines[$iIndex + $j] = $aLines[$i + $j]
        Next
        ; insert the new answer line
        $aNewLines[$iIndex + $j] = "Correct:" & $iAnswer
        ; And a blank
        $aNewLines[$iIndex + $j + 1] = ""

        _ArrayDisplay($aNewLines, $i) ; Just for display

    EndIf
Next

; Write new file
_FileWriteFromArray("NewTest.txt", $aNewLines, 1)

will change this:

1. What is the name of the cat?
*Doremon
Kitten
Cute
Dog

2.What is the name of the dog?
Doremon
Kitten
Cute
*Puppy

into this:

1. What is the name of the cat?
Doremon
Kitten
Cute
Dog
Correct:1

2.What is the name of the dog?
Doremon
Kitten
Cute
Puppy
Correct:4

I hope that helps. :)

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

I guess something like that (not tested) :

Local Const $aQuestion = StringSplit(FileRead("s.txt"), @CrLf & @CrLf, 1)
Local $sOutput = "", $iRightAnswer = 0

For $i = 1 To $aQuestion[0]
    $aSentense = StringSplit($aQuestion[$i], @CrLf, 1)

    For $i2 = 1 To $aSentense[0]
        If $i2 = 1 Then
            $sOutput &= "Question: " & StringTrimLeft($aSentense[$i2], StringInStr($aSentense[$i2], ".", 2)) & @CrLf
            ContinueLoop
        EndIf

        $sOutput &= "Choice: "

        If StringLeft($aSentense[$i2], 1) = "*" Then
            $aSentense[$i2] = StringTrimLeft($aSentense[$i2], 1)
            $iRightAnswer = $i2 - 1
        EndIf

        $sOutput &= $aSentense[$i2] & @CrLf
    Next

    $sOutput &= "Correct: " & $iRightAnswer & @CrLf & @CrLf
Next

FileWrite("s2.txt", $sOutput)

Note: Input file s.txt

Edit: too late >_<

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

This probably isn't what you need, but I whipped up a quick example for a game:

#include <GUIConstants.au3>
#include <array.au3>
#include <GuiMenu.au3>

Global $correct, $incorrect

$Form1 = GUICreate("Q&A", 479, 231)
$Label1 = GUICtrlCreateLabel("", 24, 24, 428, 41)
$Radio1 = GUICtrlCreateRadio("", 24, 72, 113, 17)
$Radio2 = GUICtrlCreateRadio("", 24, 96, 113, 17)
$Radio3 = GUICtrlCreateRadio("", 24, 120, 113, 17)
$Radio4 = GUICtrlCreateRadio("", 24, 144, 113, 17)
$Button1 = GUICtrlCreateButton("Submit", 328, 184, 75, 25, 0)
$Label2 = GUICtrlCreateLabel("Correct: 0", 24, 184, 70, 17)
$Label3 = GUICtrlCreateLabel("Incorrect: 0", 24, 200, 70, 17)

GUISetState(@SW_SHOW)

_Question()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _CheckAnswer()
            _Question()
    EndSwitch
WEnd

Func _Question()
    $iSize = IniReadSectionNames(@ScriptDir & "\data.txt")
;~     _ArrayDisplay($iSize)
    $rNum = Random(1, $iSize[0], 1)
    $c1 = IniRead(@ScriptDir & "\data.txt", $iSize[$rNum], "Choice1", "")
    $c2 = IniRead(@ScriptDir & "\data.txt", $iSize[$rNum], "Choice2", "")
    $c3 = IniRead(@ScriptDir & "\data.txt", $iSize[$rNum], "Choice3", "")
    $c4 = IniRead(@ScriptDir & "\data.txt", $iSize[$rNum], "Choice4", "")
    GUICtrlSetData($Label1, $iSize[$rNum])
    GUICtrlSetData($Radio1, $c1)
    GUICtrlSetData($Radio2, $c2)
    GUICtrlSetData($Radio3, $c3)
    GUICtrlSetData($Radio4, $c4)
EndFunc

Func _CheckAnswer()
    $c1 = GUICtrlRead($Radio1)
    $c2 = GUICtrlRead($Radio2)
    $c3 = GUICtrlRead($Radio3)
    $c4 = GUICtrlRead($Radio4)
    $question = GUICtrlRead($Label1)
    $correctAns = IniRead(@ScriptDir & "\data.txt", $question, "Correct", "")
    $ans = ""
    If $c1 = 1 Then
        $ans = ControlGetText($Form1, "", $Radio1)
    ElseIf $c2 = 1 Then
        $ans = ControlGetText($Form1, "", $Radio2)
    ElseIf $c3 = 1 Then
        $ans = ControlGetText($Form1, "", $Radio3)
    ElseIf $c4 = 1 Then
        $ans = ControlGetText($Form1, "", $Radio4)
    Else
        MsgBox(0, "Error", "You did not select an answer")
    EndIf

    If $ans = $correctAns Then
        MsgBox(0, "Congrautlations", "You got it right!")
        $correct += 1
        GUICtrlSetData($Label2, "Correct: " & $correct)
    Else
        MsgBox(0, "Incorrect", "Sorry, but the correct answer was: " & $correctAns)
        $incorrect += 1
        GUICtrlSetData($Label3, "Incorrect: " & $incorrect)
    EndIf
EndFunc

Then create a file called Data.txt and put this code in it:

[What is the name of the cat?]
Choice1=Doremon
Choice2=Kitten
Choice3=Cute
Choice4=Dog
Correct=Doremon

[what is the name of the dog?]
Choice1=Doremon
Choice2=Kitten
Choice3=Cute
Choice4=Puppy
Correct=Puppy
Link to comment
Share on other sites

Wow, Thanks so much! You guys are great. I'll check Melba23's code. I was about to create a test software too, thanks abberration again! Love programers

$Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf
Link to comment
Share on other sites

Guys, I'm having trouble with Unicode text.

Input: Con mèo tên gì à á ă â ú ớ ử ự ợ...

Output: Con m?o t?n g? ? ? ? ? ? ? ? ? ?....

I think the problem is the output file txt is saved in ASCI encoding. How do I save it in UTF-8 ?

$Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf
Link to comment
Share on other sites

  • 4 weeks later...

abberration,

It would very nice if you can help me change your function to work with both my Input and Output test files :D

Can read this file

Question: What is the name of the cat?

Choice: Doremon

Choice: Kitten

Choice: Cute

Choice: Dog

Correct:1

Question: What is the name of the dog?

Choice: Doremon

Choice: Kitten

Choice: Cute

Choice: Puppy

Correct:4

1. What is the name of the cat?

*Doremon

Kitten

Cute

Dog

2.what is the name of the dog?

Doremon

Kitten

Cute

*Puppy

It works like:

First line is question (If has "Question: " then Remove)

Next line is Answer (If has "Answer: " Then remove)

*Mutil Correct

*Mutil Correct

If has "Correct: " line then mark $LineAnswer[$number] is correct!

@CRLF is end each question

I just need an sample, I'll code on my own :D

Thanks

Edited by DucViet
$Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf
Link to comment
Share on other sites

Here something to get you started. It reads your data.txt file. When it finds the phrase "Question:", it starts putting the questions and answers into an array. For an example, I used _arraydisplay to show you the full array. I also gave an example of making it chose a random question.

On the array, you have the following:

$globalArray[<question number>][0] = the question

$globalArray[<question number>][1] = choice #1

$globalArray[<question number>][2] = choice #2

$globalArray[<question number>][3] = choice #3

$globalArray[<question number>][4] = choice #4

$globalArray[<question number>][5] = correct answer

#include <file.au3>
#include <array.au3>
$dataFile = @ScriptDir & "\data.txt"

$dataFileSize = _FileCountLines($dataFile)
Dim $globalArray[1][6]

_ReadQuestionsToArray()

_ArrayDisplay($globalArray)

$arraySize = UBound($globalArray, 1)
$randomQuestion = Round(Random(0, $arraySize - 1), 0)

MsgBox(0, "", $globalArray[$randomQuestion][0])


Func _ReadQuestionsToArray()
    $numOfQuestions = 0
    For $i = 1 to $dataFileSize Step 1
        $lineRead = FileReadLine($dataFile, $i)
        If StringInStr($lineRead, "Question:") Then
            $globalArray[$numOfQuestions][0] = StringReplace($lineRead, "Question: ", "")
            For $j = 1 to 4 step 1
                $lineRead = FileReadLine($dataFile, $i + $j)
                $globalArray[$numOfQuestions][$j] = StringReplace($lineRead, "Choice: ", "")
            Next
            $lineRead = FileReadLine($dataFile, $i + $j)
            $globalArray[$numOfQuestions][5] = StringStripWS(StringReplace($lineRead, "Correct:", ""), 8)
            $numOfQuestions += 1
            ReDim $globalArray[$numOfQuestions + 1][6]
        EndIf
    Next
    ReDim $globalArray[$numOfQuestions][6]
EndFunc
Link to comment
Share on other sites

abberration,

That's great but what if it has more or less than 4 answer? How can I count line number and do "For $j = 1 to $numOfChoice" ?

Viet

Edited by DucViet
$Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf
Link to comment
Share on other sites

abberration,

Minimum choices is 2 and maximum is 6

And there's one more problem, I have many files use this way:

1. What is the name of the cat?

*Doremon

Kitten

Cute

Dog

Can you change your example to this :D many thanks

Edited by DucViet321
$Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf
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...