Sign in to follow this  
Followers 0
Richard63

Compare 2 strings (or arrays)

13 posts in this topic

#1 ·  Posted (edited)

Hello i am trying for a few days now to get from 2 strings (comma delimeted) the part that is NEW in the 2nd string.

Ie:

$string_one = "august,Gerdi,Sanne,Eddy285" ; i read this from an php script with InetRead and write it tot a text file on local pc

$string_two = "august,Gerdi,Sanne,Eddy285" ; this is a few seconds later again read from php script and compared to the first read (the file on local pc)

i want the extra names in the 2nd string to go to a string or an aray, to pop up a msg (with toast.au3)

i tried string compare and _Diff function

i am dutch so i tried to make english remarks in the script

Please let me know if u get the Idea

Hope my english is fine enough :-)

Regards, Richard (a newbie on auto it) (my second script with it)

who_comes_online.au3

Edited by Richard63

Share this post


Link to post
Share on other sites



Hello i am trying for a few days now to get from 2 strings (comma delimeted) the part that is NEW in the 2nd string.

Ie:

$string_one = "august,Gerdi,Sanne,Eddy285" ; i read this from an php script with InetRead and write it tot a text file on local pc

$string_two = "august,Gerdi,Sanne,Eddy285" ; this is a few seconds later again read from php script and compared to the first read (the file on local pc)

i want the extra names in the 2nd string to go to a string or an aray, to pop up a msg (with toast.au3)

i tried string compare and _Diff function

i am dutch so i tried to make english remarks in the script

Please let me know if u get the Idea

Hope my english is fine enough :-)

Regards, Richard (a newbie on auto it) (my second script with it)

Forgot to put 1 more name in second compare string

$string_two = "august,Gerdi,Sanne,Eddy285,Richard" ; this is a few seconds later again read from php script and compared to the first read (the file on local pc)

so next run should popup my name is online

Share this post


Link to post
Share on other sites

Hello i am trying for a few days now to get from 2 strings (comma delimeted) the part that is NEW in the 2nd string.

Ie:

$string_one = "august,Gerdi,Sanne,Eddy285" ; i read this from an php script with InetRead and write it tot a text file on local pc

$string_two = "august,Gerdi,Sanne,Eddy285" ; this is a few seconds later again read from php script and compared to the first read (the file on local pc)

i want the extra names in the 2nd string to go to a string or an aray, to pop up a msg (with toast.au3)

i tried string compare and _Diff function

i am dutch so i tried to make english remarks in the script

Please let me know if u get the Idea

Hope my english is fine enough :-)

Regards, Richard (a newbie on auto it) (my second script with it)

Sounds like you'll want to compare each delimited string. Check out the following code:

$StringCompA = StringSplit($string_one, ",")
$StringCompB = StringSplit($string_two, ",")

From there, you'll want a for loop. My brain is a little fried at the moment, as I've been beating my head against other programming issues, but basically you'll need to use UBound() to get the size of each array for $StringCompA & $StringCompB, then run a for loop to compare each element in the arrays individually using StringCompare(), and then check for leftover strings. (This is typically simple enough, but like I said, my brain is fried atm)

When my brain is less fried, I'll post an example For loop for you if you haven't already figured it out.

Share this post


Link to post
Share on other sites

This might work for you.

Local $string_one = "august,Gerdi,Sanne,Eddy285" 
Local $string_two = "august,Gerdi,Sanne,Eddy285,Richard"

Local $sDiff = StringReplace($string_two, $string_one, "")

If $sDiff <> "" Then
    MsgBox(0, "Result", 'Difference = "' & StringTrimLeft($sDiff, 1) & '"') ; Remove leading comma.
Else
    MsgBox(0, "Result", 'Both strings the same')
EndIf

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for looking in to it Malkey

This looked promising on first run, but when i run it a second time with the second string in a different order like

Local $string_one = "august,Gerdi,Sanne,Eddy285" 
Local $string_two = "august,Gerdi,Richard,Sanne,Eddy285"

then i get a strange outcom (<is this a word?)

the outcom on this 2 strings is : Difference = "ugust,Gerdi,Richard,Sanne,Eddy285"

i dont even know where te A in august is going :-)

i use the output generated from an online script from Flashchat 6.xx

(only changed the <br> to a , to make a easier spit.)

I gonne take a coffee end wash up and take another look to both post (just awake)

Thanks so far i let you know.

Edited by Richard63

Share this post


Link to post
Share on other sites

My example post #4, will only work if ",name" is appended to end of previous string.

Try this one.

Local $string_one = "august,Gerdi,Sanne,Eddy285"
Local $string_two = "august,Gerdi,Sanne,Richard,Eddy285"

Local $sPattern = StringReplace($string_one, ",", ",|")
;ConsoleWrite($sPattern & @CRLF)
Local $sDiff = StringRegExpReplace($string_two, $sPattern, "")

If $sDiff <> "" Then
    MsgBox(0, "Result", 'Difference = "' & StringReplace($sDiff, ",", "") & '"') ; Remove comma.
Else
    MsgBox(0, "Result", 'Both strings the same')
EndIf

Share this post


Link to post
Share on other sites

Ok Malkey that worked but what if there are 2 users in the second string than i get for example the names together "PietRichard" wich is because the comma is extruded earlier i think. But i can try to figure this out :-)

Thanks Mate,

i shall see what happends if someone leaves the chat (string 2 is less names then string 1)

Share this post


Link to post
Share on other sites

Try this one.

Catering for the comings and goings on one or more names.

Local $string_one = "august,Betty,Gerdi,Wendy,Sanne,Eddy285"
Local $string_two = "august,Gerdi,Sanne,Eddy285" ; <--- Note people gone.

;Local $string_one = "august,Gerdi,Sanne,Eddy285"
;Local $string_two = "august,Richard,Gerdi,Sanne,John,Eddy285" ; <--- Note people added.

;Local $string_one = "august,Gerdi,Sanne,Eddy285"
;Local $string_two = "august,Gerdi,Sanne,Eddy285"  ; <--- Note people the same.

;Local $string_one = "august,Betty,Gerdi,Wendy,Sanne,Eddy285"
;Local $string_two = "august,Gerdi,Sanne,Eddy285,Richard" ; <--- Note win some lose some.

Local $sPattern1 = StringReplace($string_one, ",", ",?|") & ",?"
Local $sDiffAdd = StringRegExpReplace($string_two, $sPattern1, "")
;ConsoleWrite("$sPattern1 =  " & $sPattern1 & "   $sDiffAdd =  " & $sDiffAdd & @CRLF)

Local $sPattern2 = StringReplace($string_two, ",", ",?|") & ",?"
Local $sDiffGo = StringRegExpReplace($string_one, $sPattern2, "")
;ConsoleWrite("$sPattern2 =  " & $sPattern2 & "   $sDiffGo =  " & $sDiffGo & @CRLF)

If $sDiffAdd <> "" Or $sDiffGo <> "" Then
    If $sDiffAdd <> "" Then MsgBox(0, "Result", 'Person/s added = "' & _StringStripComma($sDiffAdd) & '"') ; Remove comma.
    If $sDiffGo <> "" Then MsgBox(0, "Result", 'Person/s gone = "' & _StringStripComma($sDiffGo) & '"') ; Remove comma.
Else
    MsgBox(0, "Result", 'Both strings the same')
EndIf


; Removes leading and trailing commas.
Func _StringStripComma($string)
    While StringLeft($string, 1) == ","
        $string = StringTrimLeft($string, 1)
    WEnd
    While StringRight($string, 1) == ","
        $string = StringTrimRight($string, 1)
    WEnd
    Return $string
EndFunc   ;==>_StringStripComma

Share this post


Link to post
Share on other sites

Awsome Malkey!!!!

i use your script and add the read from inetread etc.

Post script here when done.

Thanks Malkey.

Share this post


Link to post
Share on other sites

Here another method to compare 2 strings (arrays):

#include <Array.au3>
Local $string_one = "august,Betty,Gerdi,Wendy,Sanne,Eddy285"
Local $string_two = "august,Gerdi,Sanne,Eddy285" ; <--- Note people gone.

$a1 = StringSplit($string_one, ",", 2)
$a2 = StringSplit($string_two, ",", 2)
$aDiff = _ArrayDiff($a1, $a2)
_ArrayDisplay($aDiff, "_ArrayDiff")

; -------------------------------------------------
; Function:  _ArrayDiff
; Purpose:  Returns an array of elements from $avArray1 that do not occur in $avArray2
; Syntax:   _ArrayDiff(ByRef $avArray1, ByRef $avArray2 [, $f_CaseSense = 0])
;   Where:  $avArray1 = ByRef source array
;           $avArray2 = ByRef array to search for $avArray1 elements
;           $f_CaseSense (optional) = Case sensitivity as passed to StringInStr()
;               0 = not case sensitive, using the user's locale (default)
;               1 = case sensitive
;               2 = not case sensitive, using a basic/faster comparison
; Returns:  On success returns an array with [0] = count containing any elements
;               from $avArray1 that do not occur in $avArray2
;               If all elements in $avArray1 occur in $avArray2 then [0] = 0
;           On Failure returns 1-element array [0] = 0 and sets @error.
; Author:   PsaltyDS on www.autoitscript.com/forum
; Notes:    Similar to PHP array_diff function
; --------------------------------------------------
Func _ArrayDiff(ByRef $avArray1, ByRef $avArray2, $f_CaseSense = 0)
    Local $avRET[1] = [0], $sRET = ""

    If (IsArray($avArray1) = 0) Or (UBound($avArray1, 0) <> 1) Then Return SetError(1, 1, $avRET)
    If (IsArray($avArray2) = 0) Or (UBound($avArray2, 0) <> 1) Then Return SetError(1, 2, $avRET)

    Local $sArray2 = Chr(1) & _ArrayToString($avArray2, Chr(1)) & Chr(1)

    For $n = 0 To UBound($avArray1) - 1
        If Not StringInStr($sArray2, Chr(1) & $avArray1[$n] & Chr(1)) Then $sRET &= $avArray1[$n] & Chr(1)
    Next

    If StringLen($sRET) Then
        $sRET = StringTrimRight($sRET, 1)
        $avRET = StringSplit($sRET, Chr(1))
    EndIf

    Return $avRET
EndFunc  ;==>_ArrayDiff

Br,

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

#11 ·  Posted (edited)

Thanks i wil try it apart from Malky's solution.

Here is my working version for so far.

#include <Array.au3>
#include <File.au3>
#include "Toast.au3"
#include <String.au3>
SoundSetWaveVolume(100)


$loop = 1
Do

Global $sMsg, $hProgress, $aRet[2] ; needed for toast script
;read string two
Local $temp = InetRead("http://www.partyplayers.nl/chat/online2.php",1) ; 1 forces reload
Local $nBytesRead = @extended ; duno where this is for exactly
Local $bin2string = BinaryToString($temp) ; make string
Local $Trim_WhiteSpace = StringStripWS($bin2string,3) ; strip all spaces
local $string_two = _StringStripComma($Trim_WhiteSpace)

$string_two = StringReplace($string_two,"(guest) ","Gast_")
ConsoleWrite("String_two = " & $string_two & @CRLF) ; output internet read to console


; read string one
$file = FileOpen("chatters.txt", 0) ; 0 = read
Local $string_one = FileReadLine($file)                         
FileClose($file)
$string_one = StringReplace($string_one,"(guest) ","Gast_")
ConsoleWrite("String_one = " & _StringProper($string_one) & @CRLF) ; output file read to console

; our two test strings are ready now
; lets compare them

; ----------- Malkey's part starts here ------------
Local $sPattern1 = StringReplace($string_one, ",", ",?|") & ",?"
Local $sDiffAdd = StringRegExpReplace($string_two, $sPattern1, "")
;ConsoleWrite("$sPattern1 =  " & $sPattern1 & "   $sDiffAdd =  " & $sDiffAdd & @CRLF)

Local $sPattern2 = StringReplace($string_two, ",", ",?|") & ",?"
Local $sDiffGo = StringRegExpReplace($string_one, $sPattern2, "")
;ConsoleWrite("$sPattern2 =  " & $sPattern2 & "   $sDiffGo =  " & $sDiffGo & @CRLF)

If $sDiffAdd <> "" Or $sDiffGo <> "" Then
    If $sDiffAdd <> "" Then 
        ;MsgBox(0, "Result", 'Person/s added = "' & _StringStripComma($sDiffAdd) & '"') ; Remove comma.
        ; popup a toast message
        $sMsg  = _StringStripComma($sDiffAdd & @CRLF & "online")  ;
        Soundplay(@WindowsDir & "\Media\Windows Exclamation.wav", 0)
        _Toast_Set(5, 0xFF0011, 0xFFFF00, 0x0000FF, 0xFFFFFF, 10, "Arial")
        $aRet = _Toast_Show(0, "PartyPlayers Chat", $sMsg, 10)
        _Toast_Hide()

        
        EndIf
    If $sDiffGo <> "" Then 
        ;MsgBox(0, "Result", 'Person/s gone = "' & _StringStripComma($sDiffGo) & '"') ; Remove comma.
        
        EndIf
Else
    ;MsgBox(0, "Result", 'Both strings the same')
EndIf
;------------end Milkey's part here -----------------


$file = FileOpen("chatters.txt", 2)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileWriteLine($file, $string_two); record to disk from internet
FileClose($file)
ConsoleWrite("String_three = " & $string_two & @CRLF) ; output file read to console

sleep(5000)
$loop = $loop +1
Until $loop = 0


; Removes leading and trailing commas.
Func _StringStripComma($string)
    While StringLeft($string, 1) == ","
        $string = StringTrimLeft($string, 1)
    WEnd
    While StringRight($string, 1) == ","
        $string = StringTrimRight($string, 1)
    WEnd
    Return $string
EndFunc   ;==>_StringStripComma
Edited by Richard63

Share this post


Link to post
Share on other sites

My example in post #8 is incorrectly partial matching. i.e. "Jo" is matching the "Jo" in "John".

UEZ's example post #10 works correctly.

And, this _StringDiff() example is based on PsaltyDS's _ArrayDiff function.

Local $string_one = "august,Betty,Gerdi,Wendy,John,Sanne,Eddy285"
Local $string_two = "august,Gerdi,Jo,Sanne,Eddy285,Richard"

$aDiffGone = _StringDiff($string_one, $string_two)
$aDiffAdded = _StringDiff($string_two, $string_one)
MsgBox(0, "Result First Example", 'Person/s added = "' & $aDiffAdded  & '"' & @CRLF & 'Person/s gone = "' & $aDiffGone & '"')


; ------------- Second Example ---------------------
Local $string_one = "as snow Mary had a little lamb "
Local $string_two = "Little Mary had a little lamb its little fleece was white as snow"
$aDiffGone = _StringDiff($string_one, $string_two, 0, " ")
$aDiffAdded = _StringDiff($string_two, $string_one, 0, " ")
;Result of $sString2 - $sString1  = "its fleece was white"
;Result of $sString1 - $sString2  = ""

MsgBox(0, "Result Second Example", '$sString2 - $sString1  = "' & $aDiffAdded & '"' & @CRLF & _
        '$sString1 - $sString2  = "' & $aDiffGone & '"')


; -------------------------------------------------
; Function:  _StringDiff
; Purpose:  Returns a string from $sString1 that do not occur in $sString2.
;                               i.e.  _StringDiff = $sString2 - $sString1
; Syntax:   _StringDiff(ByRef $avString1, ByRef $avString2 [, $f_CaseSense = 0 ,[$sDelimiter = ","}})
;   Where:  $sString1 = Source string
;           $sString2 = String to search for in $sString1 string
;           $f_CaseSense (optional) = Case sensitivity as passed to StringInStr()
;               0 = not case sensitive, using the user's locale (default)
;               1 = case sensitive
;               2 = not case sensitive, using a basic/faster comparison
;                       $sDelimiter = One or more characters to use as delimiters (case sensitive).
; Returns:  On success returns a string from $sString1 that do not occur in $sString2
;               If all elements in $sString1 occur in $sString2 then "" is returned
; Author:   Malkey
; Notes:    Based on PsaltyDS's _ArrayDiff function on www.autoitscript.com/forum
; --------------------------------------------------
Func _StringDiff($sString1, $sString2, $f_CaseSense = 0, $sDelimiter = ",")
    Local  $sRET = ""
    $sString2 = $sDelimiter & $sString2 & $sDelimiter
    Local $aArray1 = StringSplit($sString1, $sDelimiter, 2)
    For $n = 0 To UBound($aArray1) - 1
        If Not StringInStr($sString2, $sDelimiter & $aArray1[$n] & $sDelimiter, $f_CaseSense) Then $sRET &= $aArray1[$n] & $sDelimiter
    Next
    If StringLen($sRET) Then
        $sRET = StringTrimRight($sRET, 1)
    EndIf
    Return $sRET
EndFunc   ;==>_StringDiff

Share this post


Link to post
Share on other sites

Thanks Both UEZ and Malkey

I have a correct working program now

I am currently working on shortening it purifying it.

Does anyone know on how to put html in a Toast popup (like msn)

and one version that kan stack toast's ?

i have t do now 3 times a toaster when 3 people come online almost at the same time

I rather have them popup like MSN does uppon each other.

So thanks for know i post the finished program here.who_comes_online.au3

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