Sign in to follow this  
Followers 0
AZJIO

Need a string comparison function

6 posts in this topic

I need the function to return the character position at which there was a difference.

 

StringCompare2 ( "string1", "string2)

Returns 7

 

StringCompare2 ( "letter", "string2)

Returns 1

 

StringCompare2 ( "str", "string2)

Returns 4

 

StringCompare2 ( "str1", "string2)

Returns 4

 

StringCompare2 ( "str1", "str1)

Returns 0 (full coincidence)

 

If I search in a sorted array, the index of coincidence will increase, then decrease. When reduced, there is no reason to check further.

 

 

Share this post


Link to post
Share on other sites



how long are those strings, and how many of them do you have? i'd go simple: for $i=1 to length(shorteststring); if stringmid(string1,$i,1)<>stringmid(string2,$i,1) then return $i

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Something like this?

#include <String.au3>
#include <Math.au3>

ConsoleWrite(_StringDiffIndex("string1", "STRing2") & @LF)
ConsoleWrite(_StringDiffIndex("str", "STRing2") & @LF)
ConsoleWrite(_StringDiffIndex("str1", "STRing2") & @LF)
ConsoleWrite(_StringDiffIndex("string1", "STRing1") & @LF)

Func _StringDiffIndex($s1, $s2)
    $s1 = StringSplit($s1, "")
    $s2 = StringSplit($s2, "")
    For $i = 1 To _Min($s1[0], $s2[0])
        If $s1[$i] <> $s2[$i] Then Return($i)   ; use this for case sensitive comparison   If Not $s1[$i] == $s2[$i] Then Return($i)
    Next
    Return($s1[0] = $s2[0] ? 0 : $i)
EndFunc

Edit: BTW if you need an unaccented compare (case sentively or not) I can provide code for that, e.g. to regard Җ ӂ ж as the same character.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <Array.au3>

$a = StringSplit('string1|string2|letter|str|str1', '|')
_ArraySort($a)
_ArrayDisplay($a, 'Array')

$sText = 'str1'
$res = _ArrayStringCompare($sText, $a)
MsgBox(0, 'res', $res)

Func _ArrayStringCompare($sText, $a)
    $aRE = StringSplit($sText, '')
    Local $sRE = '^'
    Local $sEndRE = ''
    For $i = 1 To $aRE[0]
        $sRE &= '(' & $aRE[$i]
        $sEndRE &= ')?'
    Next
    $sRE &= $sEndRE
    ; MsgBox(0, 'Отладчик', '$sRE = ' & $sRE)
    $idx0 = 0
    For $i = 1 To $a[0]
        ; $ind = StringRegExp($a[$i], '^(s(t(r(1)?)?)?)?', 1)
        $ind = StringRegExp($a[$i], $sRE, 2)
        $ind1 = StringLen($ind[0])
        ; MsgBox(0, $i, '$ind[0] = ' & $ind[0])
        If $ind1 >= $idx0 Then
            $idx0 = $ind1
        Else
            Return $i - 1 & '/' & $a[0] & ', ' & $a[$i - 1]
            ExitLoop
        EndIf
    Next
    Return '?'
    ; MsgBox(0, '', $sText[0] & @CRLF & StringLen($sText[0]))
EndFunc   ;==>_ArrayStringCompare
StringCompare has lexicographical comparison. Need to macro "@extended" returns the last character comparisons

Edited by AZJIO

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This should work, too.

ConsoleWrite(_StringDiffIndex("string1", "STRing2") & @LF)
ConsoleWrite(_StringDiffIndex("str", "STRing2") & @LF)
ConsoleWrite(_StringDiffIndex("str1", "STRing2") & @LF)
ConsoleWrite(_StringDiffIndex("string1", "STRing1") & @LF)
ConsoleWrite(_StringDiffIndex("str1", "str1") & @LF)
ConsoleWrite(_StringDiffIndex("letter", "string2") & @LF)

Func _StringDiffIndex($s1, $s2, $bCase = 0)
    Local $i = StringLen($s1)
    While $i
        If StringInStr($s2, StringLeft($s1, $i), $bCase) Then
            If StringLen($s1) = StringLen($s2) And $i = StringLen($s1) Then Return 0
            Return $i + 1
        EndIf
        $i -= 1
    WEnd
    Return $i + 1
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I understood how to do it. If value became less, level of coincidence went into decrease.

#include <Array.au3>

$a = StringSplit('string1|string2|letter|str|str1', '|')
; $a = StringSplit('123|122|123|124|125', '|')
; $a = StringSplit('1213|1223|123|14|15', '|')
_ArraySort($a, 0, 1)
_ArrayDisplay($a, 'Array')

$sText = 'str1'
; $sText = '123'
$res = _ArrayStringCompare($sText, $a)
MsgBox(0, 'res', @extended & '/' & $a[0] & ', ' & $res)

Func _ArrayStringCompare($sText, $a)
    $idx0 = 0
    For $i = 1 To $a[0]
        $iRes = StringCompare($sText, $a[$i])
        If $iRes <= 0 Then
            If $iRes = 0 Then
                Return SetError(0, $i, $a[$i])
            Else
                Return SetError(1)
            EndIf
        EndIf
    Next
    Return SetError(1)
EndFunc   ;==>_ArrayStringCompare

.

If the output of the search function occurs at the very beginning of the array, then "StringCompare" wins. If at the end of the array, the "StringCompare" loses.

#include <Array.au3>

Local $a[9000]
$j = 1000
For $i = 0 To 8999
    $a[$i] &= $j
    $j += 1
    ; If $j = 1001 Then $j = 1002
    If $j = 5000 Then $j = 5001
    ; If $j = 9998 Then $j = 9999
Next
; _ArraySort($a, 0, 1)
_ArrayDisplay($a, 'Array')

; $sText = '1001'
$sText = '5000'
; $sText = '9998'

$timer1 = TimerInit()
For $i = 1 To 10
    $res = _ArraySearch_InSort($a, $sText)
Next
$timer1 = Round(TimerDiff($timer1), 2)

$timer2 = TimerInit()
For $i = 1 To 10
    $res = _ArraySearch_InSort2($a, $sText)
Next
$timer2 = Round(TimerDiff($timer2), 2)

MsgBox(0, "time", '$timer1 = ' & $timer1 & ' msec' & @LF & '$timer2 = ' & $timer2 & ' msec')
; MsgBox(0, 'res', @extended & '/' & UBound($a) & ', ' & $res)

Func _ArraySearch_InSort(ByRef $a, $sText)
    For $i = 1 To UBound($a) - 1
        $iRes = StringCompare($sText, $a[$i])
        If $iRes <= 0 Then
            If $iRes = 0 Then
                Return SetError(0, $i, $a[$i])
            Else
                Return SetError(1)
            EndIf
        EndIf
    Next
    Return SetError(1)
EndFunc   ;==>_ArraySearch_InSort

Func _ArraySearch_InSort2(ByRef $a, $sText)
    For $i = 1 To UBound($a) - 1
        If $sText = $a[$i] Then Return SetError(0, $i, $a[$i])
    Next
    Return SetError(1)
EndFunc   ;==>_ArraySearch_InSort

.

Edited by AZJIO

Share this post


Link to post
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
Sign in to follow this  
Followers 0