Jump to content

compare two strings/arrays, combine their unique and common values


wuruoyu
 Share

Recommended Posts

Tried searching through the forum and can't seem to find a way do this

want compare 2 strings, they both have common values and unique values, and generate 3 seperate arrays

1st string/array = all common and unique values (1,2,3,4)
2nd string/array = booleans that indicate values that have found in string1 (Y,Y,Y,N)
3rd string/array = booleans that indicate values that have found in string2 (N,Y,Y,Y)

E.g.

string1 = "1,2,3"
string2 = "2,3,4"

str        string1    string2
1          Y               N
2          Y               Y
3          Y               Y
4          N              Y

 

Thank you in advance!

Edited by wuruoyu
Link to comment
Share on other sites

You can use the _ArrayAdd or _ArrayConcatenate funtions to combine array1 and array2 into a single array.  You can then use the _ArrayUnique function to remove duplicates.  Then it is just a matter of spinning through the array of unique values and comparing those value to the values in array 1 & 2.

#include <Constants.au3>
#include <Array.au3>

example()

Func example()
    Const $A1 = [1,2,3], _
          $A2 = [2,3,4]

    Local $aUniqueItems[0], _
          $aBool1[0], _
          $aBool2[0]

    ;Combine array 1 & 2
    _ArrayAdd($aUniqueItems, $A1)
    _ArrayAdd($aUniqueItems, $A2)

    ;Remove duplicates and sort
    $aUniqueItems = _ArrayUnique($aUniqueItems, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT)
    _ArraySort($aUniqueItems)

    ;Build boolean result arrays
    For $i = 0 To UBound($aUniqueItems) - 1
        _ArrayAdd($aBool1, (_ArraySearch($A1, $aUniqueItems[$i]) > -1 ? "Y" : "N"))
        _ArrayAdd($aBool2, (_ArraySearch($A2, $aUniqueItems[$i]) > -1 ? "Y" : "N"))
    Next

    ;Show results
    _ArrayDisplay($aUniqueItems, "Unique Items")
    _ArrayDisplay($aBool1, "String 1")
    _ArrayDisplay($aBool2, "String 2")
EndFunc

 

Edited by TheXman
Link to comment
Share on other sites

14 minutes ago, TheXman said:

You can use the _ArrayAdd or _ArrayConcatenate funtions to combine array1 and array2 into a single array.  You can then use the _ArrayUnique function to remove duplicates.  Then it is just a matter of spinning through the array of unique values and comparing those value to the values in array 1 & 2.

#include <Constants.au3>
#include <Array.au3>

example()

Func example()
    Const $A1 = [1,2,3], _
          $A2 = [2,3,4]

    Local $aUniqueItems[0], _
          $aBool1[0], _
          $aBool2[0]

    ;Combine array 1 & 2
    _ArrayAdd($aUniqueItems, $A1)
    _ArrayAdd($aUniqueItems, $A2)

    ;Remove duplicates and sort
    $aUniqueItems = _ArrayUnique($aUniqueItems, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT)
    _ArraySort($aUniqueItems)

    ;Build boolean result arrays
    For $i = 0 To UBound($aUniqueItems) - 1
        _ArrayAdd($aBool1, (_ArraySearch($A1, $aUniqueItems[$i]) > -1 ? "Y" : "N"))
        _ArrayAdd($aBool2, (_ArraySearch($A2, $aUniqueItems[$i]) > -1 ? "Y" : "N"))
    Next

    ;Show results
    _ArrayDisplay($aUniqueItems, "Unique Items")
    _ArrayDisplay($aBool1, "String 1")
    _ArrayDisplay($aBool2, "String 2")
EndFunc

 

Genius! Thanks TheXman

Link to comment
Share on other sites

Link to comment
Share on other sites

... here my 2 cents, (string functions based)

#include <Array.au3>

Local $string1 = "1,2,3,4"
Local $string2 = "2,3,4"

; merge the 2 strings and place elements in an array
Local $aAll = StringSplit($string1 & ',' & $string2, ',', 2)

; keep only unique values
Local $aUnique = _ArrayUnique($aAll) ; , 0, 0, 0, 0)

; create an empty array to store results (with headers)
Local $aResult[UBound($aUnique)][3] = [['values', 'in str1', 'in str2']]

; check presence of values in $string1 and $string2
For $i = 1 To UBound($aUnique) - 1

    ; print result on console
    ConsoleWrite($aUnique[$i] & @TAB & (StringInStr($string1, $aUnique[$i]) = True) & @TAB & (StringInStr($string2, $aUnique[$i]) = True) & @CRLF)

    ; store results in array
    $aResult[$i][0] = $aUnique[$i]
    $aResult[$i][1] = StringInStr($string1, $aUnique[$i]) = True
    $aResult[$i][2] = StringInStr($string2, $aUnique[$i]) = True

Next

; display result
_ArrayDisplay($aResult)

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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