youtuber Posted April 5, 2020 Share Posted April 5, 2020 How can I find the different word in the txt file? Suppose you have the following words in the txt file. Dim $findUniqueDictionary[5] = ['autoit', 'autoit', 'script', 'script', 'forum'] Here the 'forum' word is different, I want to save it to test_OUT.txt but I want to do this with ObjCreate('Scripting.Dictionary') ! #include <file.au3> Dim $aRecords, $sOut Global $oDict = ObjCreate('Scripting.Dictionary') $oDict.CompareMode = 1 If Not _FileReadToArray("test.txt", $aRecords) Then MsgBox(4096, "Error", " Error reading file") Exit EndIf For $x = 1 To $aRecords[0] If Not $oDict.Exists($aRecords[$x]) Then $oDict.Add($aRecords[$x], 1) $sOut &= $aRecords[$x] & @CRLF EndIf Next FileWrite("test_OUT.txt", $sOut) Link to comment Share on other sites More sharing options...
Malkey Posted April 5, 2020 Share Posted April 5, 2020 This appears to work. expandcollapse popup#include <file.au3> Local $findUniqueDictionary[5] = ['autoit', 'autoit', 'script', 'script', 'forum'] Local $sFileIn = "TestIn.txt", $sFileOut = "TestOut.txt" Local $aRecords, $sOut = "" If FileExists($sFileIn) Then FileDelete($sFileIn) _FileWriteFromArray($sFileIn, $findUniqueDictionary, Default, Default, @CRLF) ; https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dictionary-object Local $oDict = ObjCreate('Scripting.Dictionary') $oDict.CompareMode = 1 ; 1 - Performs a textual comparison. If Not _FileReadToArray($sFileIn, $aRecords) Then MsgBox(4096, "Error", " Error reading file") Exit EndIf For $x = 1 To $aRecords[0] If $oDict.Exists($aRecords[$x]) Then $oDict.Item($aRecords[$x]) += 1 ; Increment record's item value. Else $oDict.Add($aRecords[$x], 1) ; Add new record EndIf Next ; $sOut only contains those records who's item is "1" only. Meaning the record in the file appears only once. For $x = 1 To $aRecords[0] If $oDict.item($aRecords[$x]) = 1 Then $sOut &= $aRecords[$x] & @CRLF Next If FileExists($sFileOut) Then FileDelete($sFileOut) FileWrite($sFileOut, $sOut) ShellExecute($sFileOut) Sleep(3000) ; Tidy files FileDelete($sFileIn) FileDelete($sFileOut) youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted April 5, 2020 Author Share Posted April 5, 2020 Thanks yes very fast and exactly as I want Link to comment Share on other sites More sharing options...
mikell Posted April 5, 2020 Share Posted April 5, 2020 For the fun only #include <Array.au3> ;Global $aArray[10] = ["a","a","b","c","c","d","e","c","e"] Global $aArray[7] = ['script', 'autoit', 'autoit', 'script', 'script', 'forum', 'autoit'] _ArraySort($aArray) $r = StringReplace(StringRegExpReplace(_ArrayToString($aArray), '^\||(\w+)\|(?:\1\|*)+', ""), "|", @crlf) msgbox(0,"", $r) iamtheky and youtuber 2 Link to comment Share on other sites More sharing options...
youtuber Posted April 5, 2020 Author Share Posted April 5, 2020 (edited) @mikell StringRegExpReplace interesting ^\||(\w+)\|(?:\1\|*)+ https://prnt.sc/rtlybq @mikell How can I use this pattern for Notepad ++? _ArraySort _ArrayUnique no fast So I chose Scripting.Dictionary Edited April 5, 2020 by youtuber Link to comment Share on other sites More sharing options...
mikell Posted April 6, 2020 Share Posted April 6, 2020 In the previous pattern the pipes are mentioned because they were included by _ArrayToString in a txt file there are newlines . So in Notepad++ do that : - first "Edit > line operations > sort descending" (important, the regex won't work if the lines are unsorted) - then use this pattern (\w+)\R(?:\1\R*)+ and "replace all" But of course SD is a much more handy way youtuber 1 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