Sign in to follow this  
Followers 0
darkshark

compare how many items are equal in twox strings

43 posts in this topic

hi people, i need a little help

example

i have two strings

$string1 = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15"
$string2 = "06,07,08,09,10,11,12,13,14,15,16,17,18,19,20"

i need to return how many items are equal, 'separated by coma'

in this case is 10 "06,07,08,09,10,11,12,13,14 and 15"

and the strings that I will compare are in two arrays.

i tried to make this:

Func _Compare(ByRef $sArray, ByRef $sArray2)
    Local $15 = 0, $14 = 0, $13 = 0, $12 = 0, $11 = 0
    If Not IsArray($sArray) Or Not IsArray($sArray2) Then Return SetError(1)
    For $a = 1 To $sArray[0]
        Local $jogo = StringSplit($sArray[$a], ",")
        For $b = 1 To $sArray2[0]
            Local $found = 0
            For $c = 1 To $jogo[0]
                If StringInStr($sArray2[$b], $jogo[$c]) Then $found += 1
            Next
            If $found > 10 Then
                Switch $found
                    Case 15
                        $15 += 1
                    Case 14
                        $14 += 1
                    Case 13
                        $13 += 1
                    Case 12
                        $12 += 1
                    Case 11
                        $11 += 1
                EndSwitch
            EndIf
        Next
    Next
    Local $Founds[5] = [$15, $14, $13, $12, $11]
    Return $Founds
EndFunc

it's work, but is to slow

someone can help me?

thanks a lot!

Share this post


Link to post
Share on other sites



The best algorithm I could come off, in pseudo-code:

$found = 0
for($i = 0 ; $i < array1Size ; $i++){
    for($j = 0 ; $j < array2Size ; $j++){
        if($array1[$i] == $array2[$j]){
            $found++
        }
    }
}
return $found

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <array.au3>

Global $FinArray[1]

$string1 = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15"
$string2 = "06,07,08,09,10,11,12,13,14,15,16,17,18,19,20"

$sArray1 = StringSplit ($string1 , ",")
$sArray2 = StringSplit ($string2 , ",")

For $i = 1 to $sArray1[0]
    For $k = 1 to $sArray2[0]
    If $sArray1[$i] = $sArray2[$k] Then
_ArrayAdd ($FinArray , $sArray1[$i])
Endif
Next
Next

_ArrayDelete ($FinArray , 0)
_ArrayDisplay ($FinArray)
msgbox (0 , '' , ubound($FinArray))

edit: added a msgbox for the count of items

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#include <array.au3>

Global $FinArray[1]

$string1 = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15"
$string2 = "06,07,08,09,10,11,12,13,14,15,16,17,18,19,20"

$sArray1 = StringSplit ($string1 , ",")
$sArray2 = StringSplit ($string2 , ",")

For $i = 1 to $sArray1[0]
    For $k = 1 to $sArray2[0]
    If $sArray1[$i] = $sArray2[$k] Then
_ArrayAdd ($FinArray , $sArray1[$i])
Endif
Next
Next

_ArrayDelete ($FinArray , 0)
_ArrayDisplay ($FinArray)
msgbox (0 , '' , ubound($FinArray))

edit: added a msgbox for the count of items

that's pretty much what I did, but it is slow anyway, because I have to compare more than 2000 values

Share this post


Link to post
Share on other sites

Insert values into SQLite memory table(s)

and then use SELECT COUNT / DISTINCT or GROUP BY

this in autoit? because I never used this SQLite memory table

Share this post


Link to post
Share on other sites

Are the values in ascending order or is this just true for your example?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Are the values in ascending order or is this just true for your example?

only in my example!

Share this post


Link to post
Share on other sites

do the values repaeat and, if so, is it significant to your issue?

are all values 2 digit numbers?

is there a min and max range?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

<br>

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

How about this?

It sorts the two arrays in ascending order. Every value of Array1 is compared to the elements in Array2 till element2 is equal or higher. Then the next element1 is used.

Both arrays are only process once.

#include <Array.au3>
#include <Math.au3>

Global $sString1 = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15"
Global $sString2 = "06,07,08,09,10,11,12,13,14,15,16,17,18,19,20"
Global $iCount = 0
Global $iIndex1, $iIndex2 = 1
Global $aArray1 = StringSplit($sString1, ",")
Global $aArray2 = StringSplit($sString2, ",")
_ArraySort($aArray1, 0, 1)
_ArraySort($aArray2, 0, 1)
For $iIndex1 = 1 To $aArray1[0]
    For $iIndex2 = $iIndex2 To $aArray2[0]
        If $aArray1[$iIndex1] < $aArray2[$iIndex2] Then
            $iIndex2 = _Max($iIndex2 - 1, 1)
            ExitLoop
        EndIf
        If $aArray1[$iIndex1] = $aArray2[$iIndex2] Then
            $iCount = $iCount + 1
            ExitLoop
        EndIf
    Next
Next
ConsoleWrite($iCount & @CRLF)

An example with 2000 random numbers takes about 2.5 seconds to run.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

do the values repaeat and, if so, is it significant to your issue?

are all values 2 digit numbers?

is there a min and max range?

kylomas

No, the values not repeat, yes, values are always two digits

and yes, the min is 11 and max 15

How about this?

It sorts the two arrays in ascending order. Every value of Array1 is compared to the elements in Array2 till element2 is equal or higher. Then the next element1 is used.

Both arrays are only process once.

#include <Array.au3>
#include <Math.au3>

Global $sString1 = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15"
Global $sString2 = "06,07,08,09,10,11,12,13,14,15,16,17,18,19,20"
Global $iCount = 0
Global $iIndex1, $iIndex2 = 1
Global $aArray1 = StringSplit($sString1, ",")
Global $aArray2 = StringSplit($sString2, ",")
_ArraySort($aArray1, 0, 1)
_ArraySort($aArray2, 0, 1)
For $iIndex1 = 1 To $aArray1[0]
    For $iIndex2 = $iIndex2 To $aArray2[0]
        If $aArray1[$iIndex1] < $aArray2[$iIndex2] Then
            $iIndex2 = _Max($iIndex2 - 1, 1)
            ExitLoop
        EndIf
        If $aArray1[$iIndex1] = $aArray2[$iIndex2] Then
            $iCount = $iCount + 1
            ExitLoop
        EndIf
    Next
Next
ConsoleWrite($iCount & @CRLF)

An example with 2000 random numbers takes about 2.5 seconds to run.

I think it will significantly improve the time, I'll try it and post the results already!

thankyou so much!

Edit:

the function now is:

Func _Resultados(ByRef $sArray, ByRef $sArray2)
    $inicio = _Timer_Init()
    Local $15 = 0, $14 = 0, $13 = 0, $12 = 0, $11 = 0
    If Not IsArray($sArray) Or Not IsArray($sArray2) Then Return SetError(1)
    For $a = 1 To $sArray[0]
        Local $Jogados = StringSplit($sArray[$a], ",")
        For $b = 1 To $sArray2[0]
            Local $Resultados = StringSplit($sArray2[$b], ","), $c, $d = 1, $iCount = 0
            For $c = 1 To $Jogados[0]
                For $d = $d To $Resultados[0]
                    If $Jogados[$c] < $Resultados[$d] Then
                        $d = _Max($d - 1, 1)
                        ExitLoop
                    EndIf
                    If $Jogados[$c] = $Resultados[$d] Then
                        $iCount = $iCount + 1
                        ExitLoop
                    EndIf
                Next
            Next
            Switch $iCount
                Case 15
                    $15 += 1
                Case 14
                    $14 += 1
                Case 13
                    $13 += 1
                Case 12
                    $12 += 1
                Case 11
                    $11 += 1
            EndSwitch
        Next
    Next
    ConsoleWrite('comparado: ' & $sArray[0] * $sArray2[0] & 'combinações' & @CRLF & 'em: ' & _Timer_Diff($inicio))
    Local $Results[5] = [$15, $14, $13, $12, $11]
    Return $Resultados
EndFunc   ;==>_Resultados

i trying to compare one Array

$sArray
with 595 rows and another array [$sArray2] with 1944 rows

total to compare = 1156680

I'll see how long it takes and post here!

Edited by darkshark

Share this post


Link to post
Share on other sites

darkshark,

I'm missing something here...

minimum value is 11, maximum value is 15 (leaving 5 possible values) and values do NOT repeat?

Water's solution is similar to what I'm going after, if it's adequate, 'nuff said...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

darkshark,

I'm missing something here...

minimum value is 11, maximum value is 15 (leaving 5 possible values) and values do NOT repeat?

Water's solution is similar to what I'm going after, if it's adequate, 'nuff said...

kylomas

There are 15 numbers from 1 to 25 that are never repeated in the same line

Example:

Local $sArray[11]
$sArray[0] = 10
$sArray[1] = "02,03,04,06,07,09,14,15,16,18,19,20,21,24,25"
$sArray[2] = "02,03,04,06,07,09,10,11,12,15,16,21,22,24,25"
$sArray[3] = "01,02,03,04,06,07,08,13,14,16,18,21,22,24,25"
$sArray[4] = "01,02,03,04,05,07,10,11,12,13,18,19,22,23,25"
$sArray[5] = "02,03,04,06,11,13,14,15,16,17,19,20,21,23,25"
$sArray[6] = "02,05,06,07,09,11,13,15,18,19,20,21,22,23,25"
$sArray[7] = "02,03,04,05,06,09,10,12,13,15,17,19,23,24,25"
$sArray[8] = "02,04,05,06,07,08,11,12,13,15,19,20,22,23,25"
$sArray[9] = "01,03,04,05,07,09,10,11,14,15,16,17,22,23,25"
$sArray[10] = "04,05,06,07,11,12,13,17,18,19,20,21,22,23,24"

Local $SArray2[11]
$sArray2[0] = 10
$sArray2[1] = "01,03,04,09,10,11,12,14,15,16,17,19,20,21,24"
$sArray2[2] = "01,05,08,11,13,15,16,17,18,20,21,22,23,24,25"
$sArray2[3] = "01,02,03,04,05,06,08,11,12,13,16,18,20,23,24"
$sArray2[4] = "01,02,03,05,06,07,09,13,14,15,20,21,23,24,25"
$sArray2[5] = "01,02,03,05,06,08,10,11,14,15,18,21,22,23,24"
$sArray2[6] = "01,02,04,06,08,09,12,13,14,15,17,18,19,22,25"
$sArray2[7] = "02,03,04,08,09,10,13,14,15,18,19,20,21,22,23"
$sArray2[8] = "01,02,03,06,08,09,10,13,14,16,18,19,22,23,24"
$sArray2[9] = "01,02,03,04,07,10,12,15,16,17,19,20,22,23,24"
$sArray2[10] = "01,04,05,06,07,08,09,10,11,12,15,16,20,21,22"

and i need to compare the two arrays, and get how many times I had 11, 12, 13, 14 and 15 items equal in the values!

equal to this function:

Func _Resultados(ByRef $sArray, ByRef $sArray2)
    $inicio = _Timer_Init()
    Local $15 = 0, $14 = 0, $13 = 0, $12 = 0, $11 = 0
    If Not IsArray($sArray) Or Not IsArray($sArray2) Then Return SetError(1)
    For $a = 1 To $sArray[0]
        Local $Jogados = StringSplit($sArray[$a], ",")
        For $b = 1 To $sArray2[0]
            Local $Resultados = StringSplit($sArray2[$b], ","), $c, $d = 1, $iCount = 0
            For $c = 1 To $Jogados[0]
                For $d = $d To $Resultados[0]
                    If $Jogados[$c] < $Resultados[$d] Then
                        $d = _Max($d - 1, 1)
                        ExitLoop
                    EndIf
                    If $Jogados[$c] = $Resultados[$d] Then
                        $iCount = $iCount + 1
                        ExitLoop
                    EndIf
                Next
            Next
            Switch $iCount
                Case 15
                    $15 += 1
                Case 14
                    $14 += 1
                Case 13
                    $13 += 1
                Case 12
                    $12 += 1
                Case 11
                    $11 += 1
            EndSwitch
        Next
    Next
    ConsoleWrite('compared: ' & $sArray[0] * $sArray2[0] & ' combinations' & @LF & 'in: ' & _Timer_Diff($inicio)/1000 & ' seconds')
    Local $Results[5] = [$15, $14, $13, $12, $11]
    Return $Resultados
EndFunc   ;==>_Resultados
Edited by darkshark

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

darkshark,

Not enough info, eg. equal by segment, by position within segment, or by array as whole?

kylomas

*** Edit ***

Also, the solution that water proposed assumes arrays are sorted.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Here my version:

$string1 = "11,12,13,14,15"
Local $sArray[11]
$sArray[0] = 10
$sArray[1] = "02,03,04,06,07,09,14,15,16,18,19,20,21,24,25,"
$sArray[2] = "02,03,04,06,07,09,10,11,12,15,16,21,22,24,25"
$sArray[3] = "01,02,03,04,06,07,08,13,14,16,18,21,22,24,25"
$sArray[4] = "01,02,03,04,05,07,10,11,12,13,18,19,22,23,25"
$sArray[5] = "02,03,04,06,11,13,14,15,16,17,19,20,21,23,25"
$sArray[6] = "02,05,06,07,09,11,13,15,18,19,20,21,22,23,25,11,13,15,11,11,11"
$sArray[7] = "02,03,04,05,06,09,10,12,13,15,17,19,23,24,25"
$sArray[8] = "02,04,05,06,07,08,11,12,13,15,19,20,22,23,25"
$sArray[9] = "01,03,04,05,07,09,10,11,14,15,16,17,22,23,25"
$sArray[10] = "04,05,06,07,11,12,13,17,18,19,20,21,22,23,24"

$aS1 = StringSplit($string1, ",", 2)
For $h = 1 To $sArray[0]
    $string = ""
    $c = 0
    For $i = 0 To UBound($aS1) - 1
        If StringInStr("," & $sArray[$h] & ",", "," & $aS1[$i] & ",") Then
            StringReplace("," & $sArray[$h] & ",",   "," & $aS1[$i] & ",",    "," & $aS1[$i] & ",")
            $string &= $aS1[$i] & " (" & @extended & "), "
            $c += 1
        EndIf
    Next
    MsgBox(0, "Info", "Found " & $c & " equals in Array $sArray[" & $h & "] : " & StringLeft($string, StringLen($string) - 2))
Next

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

If the problem is to find out how many times total the numbers 11,12,13,14,15 appear in each line it sounds like regex is the ticket, albeit, beyond me to code it...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

If the problem is to find out how many times total the numbers 11,12,13,14,15 appear in each line it sounds like regex is the ticket, albeit, beyond me to code it...

kylomas

no, I need to know how many items are the same in both tables, like in this example:

Local $sArray[11]
$sArray[0] = 10
$sArray[1] = "02,03,04,06,07,09,14,15,16,18,19,20,21,24,25"
$sArray[2] = "02,03,04,06,07,09,10,11,12,15,16,21,22,24,25"
$sArray[3] = "01,02,03,04,06,07,08,13,14,16,18,21,22,24,25"
$sArray[4] = "01,02,03,04,05,07,10,11,12,13,18,19,22,23,25"
$sArray[5] = "02,03,04,06,11,13,14,15,16,17,19,20,21,23,25"
$sArray[6] = "02,05,06,07,09,11,13,15,18,19,20,21,22,23,25"
$sArray[7] = "02,03,04,05,06,09,10,12,13,15,17,19,23,24,25"
$sArray[8] = "02,04,05,06,07,08,11,12,13,15,19,20,22,23,25"
$sArray[9] = "01,03,04,05,07,09,10,11,14,15,16,17,22,23,25"
$sArray[10] = "04,05,06,07,11,12,13,17,18,19,20,21,22,23,24"

Local $SArray2[11]
$sArray2[0] = 10
$sArray2[1] = "01,03,04,09,10,11,12,14,15,16,17,19,20,21,24"
$sArray2[2] = "01,05,08,11,13,15,16,17,18,20,21,22,23,24,25"
$sArray2[3] = "01,02,03,04,05,06,08,11,12,13,16,18,20,23,24"
$sArray2[4] = "01,02,03,05,06,07,09,13,14,15,20,21,23,24,25"
$sArray2[5] = "01,02,03,05,06,08,10,11,14,15,18,21,22,23,24"
$sArray2[6] = "01,02,04,06,08,09,12,13,14,15,17,18,19,22,25"
$sArray2[7] = "02,03,04,08,09,10,13,14,15,18,19,20,21,22,23"
$sArray2[8] = "01,02,03,06,08,09,10,13,14,16,18,19,22,23,24"
$sArray2[9] = "01,02,03,04,07,10,12,15,16,17,19,20,22,23,24"
$sArray2[10] = "01,04,05,06,07,08,09,10,11,12,15,16,20,21,22"

comparing the $sArray[1] with $sArray2[1]

$sArray[1] = "02,03,04,06,07,09,14,15,16,18,19,20,21,24,25"
$sArray2[1] = "01,03,04,09,10,11,12,14,15,16,17,19,20,21,24"

equal numbers =

03, 04, 09, 14, 15, 16, 19, 20, 21, 24
Total = 10

i need know how many times the "Total" is 11, 12, 13, 14 and 15

and the strings are always in increasing order now

the function works, but to compare more then 100000 strings takes more than 10 minutes

this is getting very confusing!

I'll leave anyway!

Share this post


Link to post
Share on other sites

this in autoit? because I never used this SQLite memory table

Yes.

SQLite is part of standard Autoit's includes.

If I will have time I will post code for this.

I expect it will be very fast this way.

Share this post


Link to post
Share on other sites

I updated my code from post#16.

Have a look.

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

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