Jump to content

compare 3 txt files with numbers and find common ones


Recommended Posts

i got x3 text files all with numbers such as

 

1,2,3,4,5,6,24

6,2,11,13,1,24

2,3,22,23,24,6

 

is there a way i can open, read those files (this is done easily) and (this is what i need help with) compare comma delimited numbers from all x3 files, find the duplicates and display them in a messagebox ?

 

for example the output should be " found common numbers: 2,6,24"

 

thank you very much

Link to comment
Share on other sites

Link to comment
Share on other sites

asiawatcher,

This might get you started...

; strings used to simulate files...you would read your files into separate variables
local $str1 = '1,2,3,4,5,6,24'
local $str2 = '6,2,11,13,1,24'
local $str3 = '2,3,22,23,24,6'

local $astr1 = stringsplit($str1,','), $save_num

; create matching boundries to prevent "3" matching on "13", etc...
$str2 = '|' & stringreplace($str2,',','|') & '|'
$str3 = '|' & stringreplace($str3,',','|') & '|'

for $i = 0 to $astr1[0]
    if stringinstr($str2, '|' &  $astr1[$i] & '|') and stringinstr($str3, '|' &  $astr1[$i] & '|') then $save_num &= $astr1[$i] & ','
next

ConsoleWrite(stringtrimright($save_num,1) & @CRLF)

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

Link to comment
Share on other sites

:)

local $str1 = '1,2,3,4,5,6,24'
local $str2 = '6,2,11,13,1,24'
local $str3 = '2,3,22,23,24,6'

$str = $str1 & "," & $str2 & "," & $str3
Msgbox(0,"", StringRegExpReplace(StringRegExpReplace($str, '\b(\d+)\b,?(?!.*\b\1\b.*\b\1\b)', ""), ',$', "") )

Edit
BTW @kylomas, you could do it this way

local $str1 = '1,2,3,4,5,6,24'
local $str2 = '6,2,11,13,1,24'
local $str3 = '2,3,22,23,24,6'

local $astr1 = stringsplit($str1,','), $save_num
$str = $str1 & "," & $str2 & "," & $str3

for $i = 0 to $astr1[0]
    StringRegExpReplace($str, '\b' & $astr1[$i] & '\b', "" )
    If @extended = 3 Then $save_num &= $astr1[$i] & ','
next

ConsoleWrite(stringtrimright($save_num,1) & @CRLF)

 

Edited by mikell
Link to comment
Share on other sites

Hi mikell,

I originally had a regexp for this, albeit not the eye popper that you have, but realized that if you have duplicate numbers in any file (string) the results are invalid...

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

Link to comment
Share on other sites

You're right !  :lmao: Here it is

local $str1 = '1,2,3,4,5,6,24,5,5,5'
local $str2 = '6,2,11,13,1,24'
local $str3 = '2,3,22,7,7,7,23,24,6'

local $astr1 = stringsplit($str1,','), $save_num
$str = $str1 & @crlf & $str2 & @crlf  & $str3

for $i = 1 to $astr1[0]
    StringRegExpReplace($str, '(?m).*\b' & $astr1[$i] & '\b.*$', "" )
    If @extended = 3 Then $save_num &= $astr1[$i] & ','
next

ConsoleWrite(stringtrimright($save_num,1) & @CRLF)

 

Edited by mikell
Link to comment
Share on other sites

1 hour ago, mikell said:

It works... the only thing I edited is this :  for $i = 1 to $astr1[0]

 

2 hours ago, mikell said:

:)

local $str1 = '1,2,3,4,5,6,24'
local $str2 = '6,2,11,13,1,24'
local $str3 = '2,3,22,23,24,6'

$str = $str1 & "," & $str2 & "," & $str3
Msgbox(0,"", StringRegExpReplace(StringRegExpReplace($str, '\b(\d+)\b,?(?!.*\b\1\b.*\b\1\b)', ""), ',$', "") )

Edit
BTW @kylomas, you could do it this way

local $str1 = '1,2,3,4,5,6,24'
local $str2 = '6,2,11,13,1,24'
local $str3 = '2,3,22,23,24,6'

local $astr1 = stringsplit($str1,','), $save_num
$str = $str1 & "," & $str2 & "," & $str3

for $i = 0 to $astr1[0]
    StringRegExpReplace($str, '\b' & $astr1[$i] & '\b', "" )
    If @extended = 3 Then $save_num &= $astr1[$i] & ','
next

ConsoleWrite(stringtrimright($save_num,1) & @CRLF)

 

thanks this works also you are all very helpful cheers

Link to comment
Share on other sites

1 minute ago, kylomas said:

Asiawatcher,

Your criteria was that the number participate in all three files.  Are you givung new criteria?  If so, please define the exact criteria that you need.

Kylomas

i got an autoit program that creates 3 text files and i want to create a function that compares all 3 for matching numbers but to also work if one file is empty and only 2 have numbers

 

thanks and sorry for any inconvinience caused

Link to comment
Share on other sites

Hum this should work  :sweating:

#Include <Array.au3>
#include <StringConstants.au3>

local $str1 = '1,2,3,4,5,6,24,5,5,5' & @crlf
local $str2 = '6, 2,11,13,1,  24'
local $str3 = "   " ; '2,3,22,7,7,7,23,24,6'

Local $astr[3][2] = [[StringStripWS($str1, $STR_STRIPALL), StringLen($astr[0][0])], _ 
            [StringStripWS($str2, $STR_STRIPALL), StringLen($astr[1][0])], _ 
            [StringStripWS($str3, $STR_STRIPALL), StringLen($astr[2][0])]] 
;_ArrayDisplay($astr)

Local $str_ref, $n, $str_all
For $i = 0 to UBound($astr)-1
  $str_ref = (StringLen($str_ref)<$astr[$i][1]) ? $astr[$i][0] : $str_ref
  $n += ($astr[$i][1]<>0) ? 1 : 0
  $str_all &= $astr[$i][0] & @crlf
Next

local $aItems = stringsplit($str_ref,','), $save_num
for $i = 1 to $aItems[0]
    StringRegExpReplace($str_all, '(?m).*\b' & $aItems[$i] & '\b.*$', "" )
    If @extended = $n Then $save_num &= $aItems[$i] & ','
next

Msgbox(0,"", stringtrimright($save_num,1))

 

Link to comment
Share on other sites

20 minutes ago, mikell said:

Hum this should work  :sweating:

#Include <Array.au3>
#include <StringConstants.au3>

local $str1 = '1,2,3,4,5,6,24,5,5,5' & @crlf
local $str2 = '6, 2,11,13,1,  24'
local $str3 = "   " ; '2,3,22,7,7,7,23,24,6'

Local $astr[3][2] = [[StringStripWS($str1, $STR_STRIPALL), StringLen($astr[0][0])], _ 
            [StringStripWS($str2, $STR_STRIPALL), StringLen($astr[1][0])], _ 
            [StringStripWS($str3, $STR_STRIPALL), StringLen($astr[2][0])]] 
;_ArrayDisplay($astr)

Local $str_ref, $n, $str_all
For $i = 0 to UBound($astr)-1
  $str_ref = (StringLen($str_ref)<$astr[$i][1]) ? $astr[$i][0] : $str_ref
  $n += ($astr[$i][1]<>0) ? 1 : 0
  $str_all &= $astr[$i][0] & @crlf
Next

local $aItems = stringsplit($str_ref,','), $save_num
for $i = 1 to $aItems[0]
    StringRegExpReplace($str_all, '(?m).*\b' & $aItems[$i] & '\b.*$', "" )
    If @extended = $n Then $save_num &= $aItems[$i] & ','
next

Msgbox(0,"", stringtrimright($save_num,1))

 

can not initialize a variable with itself error

 
Link to comment
Share on other sites

Last try...

#Include <Array.au3>
#include <StringConstants.au3>

local $str1 = '1,2,3,4,5,6,24,5,5,5' & @crlf
local $str2 = '6, 2,11,13,1,  24'
local $str3 = "   " ; '2,3,22,7,7,7,23,24,6'

$str1 = StringStripWS($str1, $STR_STRIPALL)
$n1 = StringSplit($str1, ",")[0]-1
$str2 = StringStripWS($str2, $STR_STRIPALL)
$n2 = StringSplit($str2, ",")[0]-1
$str3 = StringStripWS($str3, $STR_STRIPALL)
$n3 = StringSplit($str3, ",")[0]-1
Local $astr[3][2] = [[$str1, $n1], [$str2, $n2], [$str3, $n3]] 
;_ArrayDisplay($astr)

Local $str_ref, $n, $str_all
For $i = 0 to UBound($astr)-1
  $str_ref = (StringSplit($str_ref, ",")[0]-1<$astr[$i][1]) ? $astr[$i][0] : $str_ref
  $n += (StringLen($astr[$i][0])<>0) ? 1 : 0
  $str_all &= $astr[$i][0] & @crlf
Next

local $aItems = stringsplit($str_ref,','), $save_num
for $i = 1 to $aItems[0]
    StringRegExpReplace($str_all, '(?m).*\b' & $aItems[$i] & '\b.*$', "" )
    If @extended = $n Then $save_num &= $aItems[$i] & ','
next

Msgbox(0,"", stringtrimright($save_num,1))

 

Edited by mikell
Link to comment
Share on other sites

here my 2 cents:

Local $file1 = '1,2,3,4,5,6,7,8,9,10'
Local $file2 = '2,5,8,10,11,12,13,14,15,16,17,18'
Local $file3 = '0,8,9'

Local $aArray[3], $sNum

$aArray[0] = Count($file1)
$aArray[1] = Count($file2)
$aArray[2] = Count($file3)

For $i = 0 To UBound($aArray) - 2
    For $s = $i + 1 To UBound($aArray) - 1
        For $n = 1 To (($aArray[$i])[2])[0]
            $sNum = "," & (($aArray[$i])[2])[$n] & ","
            StringReplace(($aArray[$s])[1], $sNum, $sNum)
            If @extended Then
                ConsoleWrite("Number " & (($aArray[$i])[2])[$n] & " from $file" & $i + 1 & " also found in $file" & $s + 1 & @CRLF)
            EndIf
        Next
    Next
Next

Func Count($sSerie)
    Local $aTemp[3]
    $sSerie = StringStripWS($sSerie, 8) ; 8 =  $STR_STRIPALL spaces
    $aTemp[2] = StringSplit($sSerie, ",")
    $aTemp[1] = "," & $sSerie & ","
    $aTemp[0] = ($aTemp[2])[0] ; not used
    Return $aTemp
EndFunc   ;==>Count

 

 

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

And another attempt.

#include <Array.au3>

Local $str1 = '1,2,3,4,5,6,5,5,24'
Local $str2 = '6,1,2,11,13,1,2,24'
Local $str3 = '2,3,22,23,24,6'

Local $sAll = $str1 & "," & $str2 & "," & $str3
;ConsoleWrite("All numbers " & $sAll & @LF)

Local $aAll = StringSplit($sAll, ",", 2)
Local $sUnique = "," & $aAll[0] & ","

; Get the unique numbers in all files
For $i = 1 To UBound($aAll) - 1
    If StringInStr($sUnique, "," & $aAll[$i] & ",") = 0 Then
        $sUnique &= $aAll[$i] & ","
    EndIf
Next
;ConsoleWrite("Unique numbers " & $sUnique & @LF)

;Unique array of numbers
Local $aUnique = StringSplit(StringTrimLeft(StringTrimRight($sUnique, 1), 1), ",", 2) ; Delete leading & trailing comas.

; Create array showing unique numbers and the number of times the unique numbers appear in each of the three files.
Local $aResult[UBound($aUnique)][4]
For $i = 0 To UBound($aResult) - 1
    $aResult[$i][0] = $aUnique[$i] ; Unique number.
    StringRegExpReplace($str1, "(\b" & $aResult[$i][0] & "\b)", "")
    $aResult[$i][1] = @extended ; Number of times the unique number appears in File 1.
    StringRegExpReplace($str2, "(\b" & $aResult[$i][0] & "\b)", "")
    $aResult[$i][2] = @extended ; Number of times the unique number appears in File 2.
    StringRegExpReplace($str3, "(\b" & $aResult[$i][0] & "\b)", "")
    $aResult[$i][3] = @extended ; Number of times the unique number appears in File 3.
Next

; Spelling it out
Local $sResults = ""
For $i = 0 To UBound($aResult) - 1
    $sResults &= 'Number "' & $aResult[$i][0] & '" appears ' & _
            ($aResult[$i][1] ? ($aResult[$i][1] = 1 ? "once" : $aResult[$i][1] & " times") & " in file 1" & ", " : "") & _
            ($aResult[$i][2] ? ($aResult[$i][2] = 1 ? "once" : $aResult[$i][2] & " times") & " in file 2" & ", " : "") & _
            ($aResult[$i][3] ? ($aResult[$i][3] = 1 ? "once" : $aResult[$i][3] & " times") & " in file 3" & ", " : "") & @LF
Next
ConsoleWrite($sResults & @LF)
_ArrayDisplay($aResult, "Number of occurrences in each file.", "", 2, "|", "Number|File 1|File 2|File 3")

 

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