Jump to content

compare 3 txt files with numbers and find common ones


Recommended Posts

asiawatcher,

Here you go.  Note: File#1 (string#1) cannot be the blank file.  If there is the possibility that file#1 could be the blank file than you can add code to handle that.

#include <array.au3>

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

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

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

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

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

kylomas 

edit: malkey's code raises a good question.

What do you want to report if file1 = 1,1,2,3,4 and file 2 = 1,1,5,6,7?

Edited by kylomas
corrected increment variable (from '0' to '1')

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

10 hours ago, kylomas said:

asiawatcher,

Here you go.  Note: File#1 (string#1) cannot be the blank file.  If there is the possibility that file#1 could be the blank file than you can add code to handle that.

#include <array.au3>

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

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

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

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

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

kylomas 

edit: malkey's code raises a good question.

What do you want to report if file1 = 1,1,2,3,4 and file 2 = 1,1,5,6,7?

hi all thanks for your replies, each file could be the blank one not just file3  maybe sometimes both file3 and 2 could be blank or file1 and 2 could be blank but one will surely have contents we just dont know which one at any given time

Also each file has UNIQUE numbers you will not find the same number again in the same file but it can be in the other 2 files

 

cheers

Link to comment
Share on other sites

asiawatcher,

Try this...

Wait one...OK, try this...not tested completely...

#include <array.au3>

local $aSizes[3][2]
$aSizes[0][0] = FileGetSize(@scriptdir & '\f1.txt')
$aSizes[0][1] = '\f1.txt'
$aSizes[1][0] = FileGetSize(@scriptdir & '\f2.txt')
$aSizes[1][1] = '\f2.txt'
$aSizes[2][0] = FileGetSize(@scriptdir & '\f3.txt')
$aSizes[2][1] = '\f3.txt'
_arraysort($aSizes,1)

local $str1 = fileread(@scriptdir & $aSizes[0][1])
local $str2 = fileread(@scriptdir & $aSizes[1][1])
local $str3 = fileread(@scriptdir & $aSizes[2][1])

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

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

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

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

kylomas

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

Link to comment
Share on other sites

...even my code from post #19 it works and meets the requirements, ..... and not only... :)
in addition it can also function to compare not only three files, but even more than 3 files if necessary.
p.s.
I let the output report a bit "incomplete" cause is not very clear if the numbers sought must appear in all the files (in all three in this case) or it's ok even if they appear in more than just one file even if not in all three?

 

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

6 hours ago, kylomas said:

asiawatcher,

Try this...

Wait one...OK, try this...not tested completely...

#include <array.au3>

local $aSizes[3][2]
$aSizes[0][0] = FileGetSize(@scriptdir & '\f1.txt')
$aSizes[0][1] = '\f1.txt'
$aSizes[1][0] = FileGetSize(@scriptdir & '\f2.txt')
$aSizes[1][1] = '\f2.txt'
$aSizes[2][0] = FileGetSize(@scriptdir & '\f3.txt')
$aSizes[2][1] = '\f3.txt'
_arraysort($aSizes,1)

local $str1 = fileread(@scriptdir & $aSizes[0][1])
local $str2 = fileread(@scriptdir & $aSizes[1][1])
local $str3 = fileread(@scriptdir & $aSizes[2][1])

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

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

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

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

kylomas

excellent job cheers

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