Jump to content

help _ArrayConcatenate in 2D arrays


 Share

Recommended Posts

I have 2 text files containing all the links

file 1 contains

http://www.link1.com

http://www.link2.com

http://www.link3.com

http://www.link4.com

http://www.link5.com

file 2 contains

http://www.link3.com

http://www.link4.com

http://www.link5.com

http://www.link6.com

http://www.link7.com

I would like to use _ArrayConcatenate to combine these two files, then use _ArrayUnique to remove the duplicate values. And finally I want to use _ArrayConcatenate (2D) to retrieve the values of file 2 and save it

I have learned and do not know how to use it, please help me, I am very thankful

Edited by JonnyQuy
Link to comment
Share on other sites

... something like this should do:

#include <File.au3>
#include <Array.au3>

; disk files containig links
Local $sFilename1 = "links1.txt"
Local $sFilename2 = "links2.txt"
Local $sNewFile = "Result.txt"

; get content from files to arrays
Local $aArray1 = FileReadToArray($sFilename1)
Local $aArray2 = FileReadToArray($sFilename2)

; content of $aArray2 is merged into $aArray1
_ArrayConcatenate($aArray1, $aArray2)

; create a new array containing only unique elements
$aArray3 = _ArrayUnique($aArray1, 0, 0, 0, 0)

_ArrayDisplay($aArray3, "Debug")

; save the content of $aArray3 to a disk file
_FileWriteFromArray($sNewFile, $aArray3)

 

 

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

well, time ago I wrote a function that could be useful to solve your problem

something like this should work:

#include <File.au3>
#include <Array.au3>

; disk files containig links
Local $sFilename1 = "links1.txt"
Local $sFilename2 = "links2.txt"
Local $sNewFile = "Result.txt"

; get content from files to arrays
Local $aArray1 = FileReadToArray($sFilename1)
Local $aArray2 = FileReadToArray($sFilename2)

; create a new 2D array containing elements as following:
; column [0] : elements contained only in file 1
; column [1] : elements contained only in file 2
; column [2] : elements contained in both files
Local $aArray3 = _Separate($aArray1, $aArray2)

_ArrayDisplay($aArray3, "Debug")

; count the number of links found
Local $iRows = 0
For $i = 0 To UBound($aArray3) -1
    If $aArray3[$i][1] <> "" Then
        $iRows += 1
    Else
        ExitLoop
    EndIf
Next

If $iRows Then ; if there are wanted links
    ; extract found links
    Local $aArray4 = _ArrayExtract($aArray3, -1, $iRows - 1, 1, 1)
    _ArrayDisplay($aArray4, "found links")

    ; save links to disk file
    _FileWriteFromArray($sNewFile, $aArray4)
Else
    MsgBox(0, "Debug", "no links found")
EndIf

Func _Separate(ByRef $in0, ByRef $in1)
    $in0 = _ArrayUnique($in0, 0, Default, Default, 0)
    $in1 = _ArrayUnique($in1, 0, Default, Default, 0)
    Local $z[2] = [UBound($in0), UBound($in1)], $low = 1 * ($z[0] > $z[1]), $aTemp[$z[Not $low]][3], $aOut = $aTemp, $aNdx[3]
    For $i = 0 To $z[Not $low] - 1
        If $i < $z[0] Then $aTemp[$i][0] = $in0[$i]
        If $i < $z[1] Then $aTemp[$i][1] = $in1[$i]
    Next
    For $i = 0 To $z[$low] - 1
        $x = _ArrayFindAll($aTemp, $aTemp[$i][$low], 0, 0, 1, 0, Not $low)
        If Not @error Then ; both
            For $j = 0 To UBound($x) - 1
                $aTemp[$x[$j]][2] = 1
            Next
            $aOut[$aNdx[2]][2] = $aTemp[$i][$low]
            $aNdx[2] += 1
        Else ; only in $low
            $aOut[$aNdx[$low]][$low] = $aTemp[$i][$low]
            $aNdx[$low] += 1
        EndIf
    Next
    For $i = 0 To $z[Not $low] - 1
        If $aTemp[$i][2] <> 1 Then
            $aOut[$aNdx[Not $low]][Not $low] = $aTemp[$i][Not $low]
            $aNdx[Not $low] += 1
        EndIf
    Next
    ReDim $aOut[_ArrayMax($aNdx)][3]
    Return $aOut
EndFunc   ;==>_Separate

 

Edited by Chimp
UBound($aArray3) -1

 

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

Thank you I have found a short but not how to delete the same value

Local $filenew = @ScriptDir & "\links1.txt"
Local $filesave = @ScriptDir & "\links2.txt"
$new = _FileReadToArray($filenew)
$save = FileRead($filesave)
For $a = 1 To UBound($new) - 1
    If StringInStr($save, $new[$a]) = True Then
        _ArrayDelete($new[$a])
    EndIf
Next
 

Link to comment
Share on other sites

.... from what I see from the screenshot, all links contained in the links2.txt file are also contained in the links1.txt file, so they are considered "duplicate" and consequently discarded. For what I understud, you need to keep only links that are present in links2.txt but not also in the links1.txt file. .....isn't  so?

 

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

if you want to keep and save only content of first column (column 0) of the array, the you have to change the "index" in the code so to point to column 0 of the array instead of column 1. for convenience I've changed a bit the code inserting a new variable called $iWantedColum, so you can set that variable to the column you are interested to keep. change this portion of code in the main script.

_ArrayDisplay($aArray3, "Debug")

Local $iWantedColumn = 0 ; column to keep (0, 1, or 2)

; count the number of links found
Local $iRows = 0
For $i = 0 To UBound($aArray3) - 1
    If $aArray3[$i][$iWantedColumn] <> "" Then
        $iRows += 1
    Else
        ExitLoop
    EndIf
Next

If $iRows Then ; if there are wanted links
    ; extract found links
    Local $aArray4 = _ArrayExtract($aArray3, -1, $iRows - 1, $iWantedColumn, $iWantedColumn)
    _ArrayDisplay($aArray4, "found links")

    ; save links to disk file
    _FileWriteFromArray($sNewFile, $aArray4)
Else
    MsgBox(0, "Debug", "no links found")
EndIf

 

 

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