DucViet321 Posted April 1, 2013 Share Posted April 1, 2013 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?*DoremonKittenCuteDog2.what is the name of the dog?DoremonKittenCute*Puppyand output:File .txtQuestion: What is the name of the cat?Choice: DoremonChoice: KittenChoice: CuteChoice: DogCorrect:1Question: What is the name of the dog?Choice: DoremonChoice: KittenChoice: CuteChoice: PuppyCorrect:4With "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 More sharing options...
FireFox Posted April 1, 2013 Share Posted April 1, 2013 Hi, Take a look at the _FileReadToArray and StringInStr functions. Br, FireFox. DucViet321 1 Link to comment Share on other sites More sharing options...
DucViet321 Posted April 1, 2013 Author Share Posted April 1, 2013 Sorry but thats difficult to me, can you give me some example? Tks alot! $Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 1, 2013 Moderators Share Posted April 1, 2013 DucViet, Something like this: expandcollapse popup#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 DucViet321 1  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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
FireFox Posted April 1, 2013 Share Posted April 1, 2013 (edited) 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 April 1, 2013 by FireFox DucViet321 1 Link to comment Share on other sites More sharing options...
abberration Posted April 1, 2013 Share Posted April 1, 2013 This probably isn't what you need, but I whipped up a quick example for a game: expandcollapse popup#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 DucViet321 1 Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
DucViet321 Posted April 2, 2013 Author Share Posted April 2, 2013 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 More sharing options...
DucViet321 Posted April 4, 2013 Author Share Posted April 4, 2013 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 More sharing options...
FireFox Posted April 4, 2013 Share Posted April 4, 2013 (edited) Like this : #include <FileConstants.au3> Local Const $hFile = FileOpen("s2.txt", BitOR($FO_OVERWRITE, $FO_UTF8)) FileWrite($hFile, $sOutput) FileClose($hFile) Br, FireFox. Edited April 4, 2013 by FireFox Link to comment Share on other sites More sharing options...
AZJIO Posted April 4, 2013 Share Posted April 4, 2013 DucVietFileGetEncoding DucViet321 1 My other projects or all Link to comment Share on other sites More sharing options...
DucViet321 Posted April 5, 2013 Author Share Posted April 5, 2013 Tks, I changed to _ArrayDisplayModified in this topic $Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf Link to comment Share on other sites More sharing options...
DucViet321 Posted May 3, 2013 Author Share Posted May 3, 2013 (edited) abberration, It would very nice if you can help me change your function to work with both my Input and Output test files 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 Thanks Edited May 3, 2013 by DucViet $Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf Link to comment Share on other sites More sharing options...
abberration Posted May 3, 2013 Share Posted May 3, 2013 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 DucViet321 1 Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
DucViet321 Posted May 3, 2013 Author Share Posted May 3, 2013 (edited) 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 May 3, 2013 by DucViet $Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf Link to comment Share on other sites More sharing options...
abberration Posted May 3, 2013 Share Posted May 3, 2013 I would have to re-think the code. What's the maximum number of choices will you have? Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
DucViet321 Posted May 4, 2013 Author Share Posted May 4, 2013 (edited) abberration,Minimum choices is 2 and maximum is 6And there's one more problem, I have many files use this way:1. What is the name of the cat?*DoremonKittenCuteDogCan you change your example to this many thanks Edited May 4, 2013 by DucViet321 $Money = ControlGetMoney(@Life,"People","Pocket4")If $Money Then $Rich = TrueElse $Risk = True _RunAwayFromPolice("Fastest")EndIf Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now