bhodi78 Posted October 8, 2009 Share Posted October 8, 2009 I whont replace all whole word of the "a.txt" file example of a.txt file: My mommy always said|there were no monsters. She's operating at|a completely adult capacity. -What about her memories? |There are gaps. Synaptic dissonance. She's freaked! #include <Array.au3> #include <File.au3> $nameS="C:\a.txt" $nameD="C:\a(TT).txt" FileCopy($nameS,$nameD,1) ;overwrite existing files Local $Wordslist[7] =["monsters","what","memories","There","freaked","my","mommy"] Local $translated[7]=["mostro","che cosa","ricordi","là","anormale","mio","mamma"] For $i=0 To UBound($Wordslist)-1 $str=" "&$Wordslist[$i]&"("&$translated[$i]&") " $w =" " & $Wordslist[$i] & " " $retval = _ReplaceStringInFile($nameD,$w,$str) If $retval = -1 then msgbox(0, "ERROR", "Not be replaced in file") Exit Else ;MsgBox(0,$w,$str) Endif Next MsgBox(0, "Finish!","Finish!") the result is: My mommy(mamma) always said|there were no monsters. She's operating at|a completely adult capacity. -What about her memories? |There are gaps. Synaptic dissonance. She's freaked! The result is not what I wanted, in fact the words ("monsters","what","memories","There","freaked","my") have not been replaced. I solved the problem in the following code, but is very slow for a large list of words. Have a better solution? #include <Array.au3> #include <File.au3> Local $myChar[11]=["'","-","!","?",",",";",":"," ","}",".","|"] $nameS="C:\a.txt" $nameD="C:\a(TT).txt" FileCopy($nameS,$nameD,1) ;overwrite existing files Local $Wordslist[7] =["monsters","what","memories","There","freaked","my","mommy"] Local $translated[7]=["mostro","che cosa","ricordi","là","anormale","mio","mamma"] For $i=0 To UBound($Wordslist)-1 For $cs In $myChar For $ce In $myChar $w =$cs & $Wordslist[$i] & $ce $str=$cs & $Wordslist[$i]&"("&$translated[$i]&")" & $ce $retval = _ReplaceStringInFile($nameD,$w,$str) If $retval = -1 then msgbox(0, "ERROR", "Not be replaced in file") Exit Else ;MsgBox(0,$w,$str) Endif Next Next Next MsgBox(0, "Finish!","Finish!") the result is: My mommy(mamma) always said|There(là) were no monsters(mostro). She's operating at|a completely adult capacity. -what(che cosa) about her memories(ricordi)? |There(là) are gaps. Synaptic dissonance. She's freaked(anormale)! Note that the first word ("My") has not yet replaced. I hope you can help me thanks. Bye Link to comment Share on other sites More sharing options...
dantay9 Posted October 8, 2009 Share Posted October 8, 2009 Here is a way that will get you by. It isn't perfect at all though. #include <Array.au3> #include <File.au3> Global $Wordslist[7] = ["monsters", "what", "memories", "There", "freaked", "my", "mommy"] Global $translated[7] = ["mostro", "che cosa", "ricordi", "là", "anormale", "mio", "mamma"] Global $nameS = "a.txt" Global $nameD = "a(TT).txt" $Output = FileRead($nameS) For $i = 0 To UBound($Wordslist) - 1 $Output = StringReplace($Output, $Wordslist[$i], $translated[$i]) Next If FileExists($nameD) Then FileDelete($nameD) FileWrite($nameD, $Output) If you want something that is perfect, you should probably use StringRegExp. It is a bit difficult in the beginning, but once you understand it, it can prove very useful. Link to comment Share on other sites More sharing options...
99ojo Posted October 9, 2009 Share Posted October 9, 2009 (edited) Here is a way that will get you by. It isn't perfect at all though. #include <Array.au3> #include <File.au3> Global $Wordslist[7] = ["monsters", "what", "memories", "There", "freaked", "my", "mommy"] Global $translated[7] = ["mostro", "che cosa", "ricordi", "là", "anormale", "mio", "mamma"] Global $nameS = "a.txt" Global $nameD = "a(TT).txt" $Output = FileRead($nameS) For $i = 0 To UBound($Wordslist) - 1 $Output = StringReplace($Output, $Wordslist[$i], $translated[$i]) Next If FileExists($nameD) Then FileDelete($nameD) FileWrite($nameD, $Output) If you want something that is perfect, you should probably use StringRegExp. It is a bit difficult in the beginning, but once you understand it, it can prove very useful. Hi, here we go: #include <File.au3> $nameS="C:\a.txt" $nameD="C:\a(TT).txt" Local $Wordslist[7] =["monsters","what","memories","There","freaked","my","mommy"] Local $translated[7]=["mostro","che cosa","ricordi","là","anormale","mio","mamma"] Local $arfile _FileReadToArray ($nameS, $arfile) For $i = 1 To UBound ($arfile) - 1 For $j = 0 To UBound ($Wordslist) - 1 $pattern = "((?i)\b" & $Wordslist [$j] & ")" $var = StringRegExpReplace ($arfile [$i], $pattern, $Wordslist [$j] & "(" & $translated [$j] & ")") $arfile [$i] = $var Next Next _FileWriteFromArray ($nameD, $arfile, 1) MsgBox(0, "Finish!","Finish!") ShellExecute ("notepad.exe", $nameD) ;-)) Stefan Edited October 9, 2009 by 99ojo Link to comment Share on other sites More sharing options...
bhodi78 Posted October 9, 2009 Author Share Posted October 9, 2009 Thanks to all, the solution with StringRegExpReplace is excellent. 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