Jump to content

How to find dissimilar dictionary?


Recommended Posts

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

This appears to work.

#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)

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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  :)

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...