Jump to content

Recommended Posts

You'll need a dictionary or a linguist expert to differentiate what repetition should be considered correct and what a typo because pool or tool or bee are all correct unlike shiverr or errror heh :D

Edit: Meh, nevermind. It's StringRegExpReplace($sStr, '(?i)(\w)\1+', '\1').

Edited by Authenticity
Link to comment
Share on other sites

I dont know how to use StringRegExpReplace but felt like a challenge using other methods

$sString = "Potato"
$sString2 = "Mississippi"
$sString3 = "The quick brown fox jumps over the lazy dog"

ConsoleWrite($sString & " = " & _removeDuplicateChar($sString) & @CR)
ConsoleWrite($sString2 & " = " & _removeDuplicateChar($sString2) & @CR)
ConsoleWrite($sString3 & " = " & _removeDuplicateChar($sString3) & @CR)


Func _removeDuplicateChar($sString)
    Local $sNewString, $deleteChar
    Do
    $deleteChar = StringLeft($sString,1)
    $sNewString = $sNewString & $deleteChar
    If $deleteChar <> " " Then
        $sString = StringReplace($sString,$deleteChar,"")
    Else
        $sString = StringTrimLeft($sString,1)
    EndIf
    Until  StringLen($sString) = 0
    Return StringStripWS($sNewString,4)
EndFunc

Potato = Pota
Mississippi = Misp
The quick brown fox jumps over the lazy dog = The quick brown fx jmps v  lazy dg
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

To add limit of 26 chars to my method just change line

Until StringLen($sString) = 0

to

Until StringLen($sString) = 0 or StringLen($sNewString) = 26

#include <Array.au3>

$sString = "The quick brown fox jumps over the lazy dog abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedbca"

$begin1 = TimerInit()
_removeDuplicateChar($sString)
$dif1 = TimerDiff($begin1)

$begin2 = TimerInit()
_StripWord($sString)
$dif2 = TimerDiff($begin2)

ConsoleWrite("_removeDuplicateChar = " & $dif1 & @CR & "_StripWord = " & $dif2)

removeDuplicateChar = 0.593371503920191
_StripWord = 50.7503048571816

Func _removeDuplicateChar($sString)
    Local $sNewString, $deleteChar
    Do
    $deleteChar = StringLeft($sString,1)
    $sNewString = $sNewString & $deleteChar
    If $deleteChar <> " " Then
        $sString = StringReplace($sString,$deleteChar,"")
    Else
        $sString = StringTrimLeft($sString,1)
    EndIf
    Until  StringLen($sString) = 0  or StringLen($sNewString) = 26
    Return StringStripWS($sNewString,4)
EndFunc


;~ Opt("MustDeclareVars", 1)

;~ MsgBox(0, "", _StripWord("The quick brown fox jumps over the lazy dog abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedbca"))

Func _StripWord($sString)
    $sString = StringReplace($sString," ","")
    Local $aString = StringSplit($sString,"")
;_ArrayDisplay($aString)
    For $i1 = 1 To $aString[0]
        For $i2 = 1 To $aString[0]
            If $i1 <> $i2 Then
                If $aString[$i1] = $aString[$i2] Then $aString[$i2] = ""
            EndIf
        Next
    Next
;_ArrayDisplay($aString)
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc
Edited by Yoriz
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

#include <Array.au3>
Opt("MustDeclareVars", 1)

Dim $sStr = 'The quick brown fox jumps over the lazy dog abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedbca'
Dim $iInit
Dim $sResult

$iInit = TimerInit()
For $i = 1 To 20
    $sResult = _StripWord1($sStr)
Next
ConsoleWrite('_StripWord1: ' & $sResult & @TAB & TimerDiff($iInit) & @LF)

$iInit = TimerInit()
For $i = 1 To 20
    $sResult = _StripWord2($sStr)
Next
ConsoleWrite('_StripWord2: ' & $sResult & @TAB & TimerDiff($iInit) & @LF)
Exit

Func _StripWord1($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $aString = StringSplit($sString,"")
    For $i1 = 1 To $aString[0]
        For $i2 = 1 To $aString[0]
            If $i1 <> $i2 Then
                If $aString[$i1] = $aString[$i2] Then $aString[$i2] = ""
            EndIf
        Next
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc


Func _StripWord2($sString)
    Local $sPatt = '(?i)((.).*?)\2'
    
    Do
        $sString = StringRegExpReplace($sString, $sPatt, '\1')
    Until Not @extended

    Return $sString
EndFunc

Link to comment
Share on other sites

I'm getting some odd returns from these timer loops tonight, such as negative numbers... (?)

_StripWord3 is a mod of Thanubis's code that seems fast?

#include <Array.au3>

Dim $sStr = 'The quick brown fox jumps over the lazy dog abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedbca'

$iInit = TimerInit()
For $i = 1 To 50
    $sResult1 = _StripWord1($sStr)
Next
$t1 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult2 = _StripWord2($sStr)
Next
$t2 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult3 = _StripWord3($sStr)
Next
$t3 = Int(TimerDiff($iInit))

MsgBox(1,"", $sResult1 & @CRLF & "SW1: " & $t1 & @CRLF & $sResult2 & @CRLF & "SW2: " & $t2 & @CRLF & $sResult3 & @CRLF & "SW3: " & $t3)
Exit

Func _StripWord1($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $aString = StringSplit($sString,"")
    For $i1 = 1 To $aString[0]
        For $i2 = 1 To $aString[0]
            If $i1 <> $i2 Then
                If $aString[$i1] = $aString[$i2] Then $aString[$i2] = ""
            EndIf
        Next
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc


Func _StripWord2($sString)
    Local $sPatt = '(?i)((.).*?)\2'
    
    Do
        $sString = StringRegExpReplace($sString, $sPatt, '\1')
    Until Not @extended

    Return $sString
EndFunc

Func _StripWord3($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $j, $Array[256], $aString = StringSplit($sString,"")
    For $i = 1 To $aString[0]
        $j = Asc($aString[$i])
        If $array[$j] Then 
            $aString[$i] = ""
        Else
            $array[$j] = 1
        EndIf
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc
Link to comment
Share on other sites

Actually, Yoriz's version is pretty fast:

#include <Array.au3>

Dim $sStr = 'The quick brown fox jumps over the lazy dog abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedbca'

$iInit = TimerInit()
For $i = 1 To 50
    $sResult1 = _StripWord1($sStr)
Next
$t1 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult2 = _StripWord2($sStr)
Next
$t2 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult3 = _StripWord3($sStr)
Next
$t3 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult4 = _StripWord4($sStr)
Next
$t4 = Int(TimerDiff($iInit))

MsgBox(1,"", $sResult1 & @CRLF & "SW1: " & $t1 & @CRLF & $sResult2 & @CRLF & "SW2: " & $t2 & @CRLF & $sResult3 & @CRLF & "SW3: " & $t3 & @CRLF & $sResult4 & @CRLF & "SW4: " & $t4)
Exit

Func _StripWord1($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $aString = StringSplit($sString,"")
    For $i1 = 1 To $aString[0]
        For $i2 = 1 To $aString[0]
            If $i1 <> $i2 Then
                If $aString[$i1] = $aString[$i2] Then $aString[$i2] = ""
            EndIf
        Next
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc


Func _StripWord2($sString)
    Local $sPatt = '(?i)((.).*?)\2'
    
    Do
        $sString = StringRegExpReplace($sString, $sPatt, '\1')
    Until Not @extended

    Return $sString
EndFunc

Func _StripWord3($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $j, $Array[256], $aString = StringSplit($sString,"")
    For $i = 1 To $aString[0]
        $j = Asc($aString[$i])
        If $array[$j] Then
            $aString[$i] = ""
        Else
            $array[$j] = 1
        EndIf
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc

Func _StripWord4($sString)
    Local $sNewString, $deleteChar
    Do
    $deleteChar = StringLeft($sString,1)
    $sNewString = $sNewString & $deleteChar
    If $deleteChar <> " " Then
        $sString = StringReplace($sString,$deleteChar,"")
    Else
        $sString = StringTrimLeft($sString,1)
    EndIf
    Until  StringLen($sString) = 0
    Return StringStripWS($sNewString,4)
EndFunc

You can modify it a little bit to consider a space as deleted character as well.

Link to comment
Share on other sites

Actually, Yoriz's version is pretty fast:

You can modify it a little bit to consider a space as deleted character as well.

You're right. Nice job, Yoriz. Like below, it behaves just like the OP's, and is the fastest.

Func _StripWord4($sString)
    Local $sNewString, $deleteChar
    $sString = StringUpper(StringReplace($sString," ",""))
    Do
        $deleteChar = StringLeft($sString,1)
        $sNewString &= $deleteChar
        $sString = StringReplace($sString,$deleteChar,"")
    Until  StringLen($sString) = 0
    Return $sNewString
EndFunc
Link to comment
Share on other sites

  • Moderators

Seeing that you won't have but 255 characters if you're dealing with standard ASCII characters, then concatenation maybe just as fast if not faster than the other methods.

Func _rep_dupechars($s_text, $i_case_sensitive = 0)
    Local $a_split = StringSplit($s_text, ""), $s_ret = ""
    For $i = 1 To $a_split[0]
        If StringInStr($s_ret, $a_split[$i], $i_case_sensitive) = 0 Then
            $s_ret &= $a_split[$i]
        EndIf
    Next
    Return $s_ret
EndFunc

BTW... StripWord4() is faster, but... doesn't show the correct results for me.

Edit:

The StripWord4() I'm talking about is in Authenticity's example.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yes, a subtle difference but still this approach is faster:

#include <Array.au3>

Dim $sStr = 'The quick brown fox jumps over the lazy dog abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedbca'

$iInit = TimerInit()
For $i = 1 To 50
    $sResult1 = _StripWord1($sStr)
Next
$t1 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult2 = _StripWord2($sStr)
Next
$t2 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult3 = _StripWord3($sStr)
Next
$t3 = Int(TimerDiff($iInit))

$iInit = TimerInit()
For $i = 1 To 50
    $sResult4 = _StripWord4($sStr)
Next
$t4 = Int(TimerDiff($iInit))

MsgBox(1,"", $sResult1 & @CRLF & "SW1: " & $t1 & @CRLF & $sResult2 & @CRLF & "SW2: " & $t2 & @CRLF & $sResult3 & @CRLF & "SW3: " & $t3 & @CRLF & $sResult4 & @CRLF & "SW4: " & $t4)
Exit

Func _StripWord1($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $aString = StringSplit($sString,"")
    For $i1 = 1 To $aString[0]
        For $i2 = 1 To $aString[0]
            If $i1 <> $i2 Then
                If $aString[$i1] = $aString[$i2] Then $aString[$i2] = ""
            EndIf
        Next
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc


Func _StripWord2($sString)
    Local $sPatt = '(?i)((.).*?)\2'
   
    Do
        $sString = StringRegExpReplace($sString, $sPatt, '\1')
    Until Not @extended

    Return $sString
EndFunc

Func _StripWord3($sString)
    $sString = StringUpper(StringReplace($sString," ",""))
    Local $j, $Array[256], $aString = StringSplit($sString,"")
    For $i = 1 To $aString[0]
        $j = Asc($aString[$i])
        If $array[$j] Then
            $aString[$i] = ""
        Else
            $array[$j] = 1
        EndIf
    Next
    $sString = _ArrayToString($aString, "", 1)
    Return $sString
EndFunc

Func _StripWord4($sString)
    Local $sNewString, $deleteChar
    
    While StringLen($sString)
        $deleteChar = StringLeft($sString,1)
        $sNewString = $sNewString & $deleteChar
        $sString = StringReplace($sString,$deleteChar,"")
    WEnd
    
    Return StringStripWS($sNewString,4)
EndFunc
Link to comment
Share on other sites

I usually use post preview to see how it looks like, if you mean by protection that it doesn't ruin the text inside it when it gets modified I have no clue but I know they work pretty nice together. Just something I've notice a few times is that sometime when you try to modify it the codebox will turn into <div> </ div> or something :D

Edit: typo.

Edited by Authenticity
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...