Sign in to follow this  
Followers 0
BitByteBit

Combine 2 arrays, ensure no duplicates

5 posts in this topic

#1 ·  Posted (edited)

Say I read 2 .ini files with IniReadSection and want to merge the two 2D arrays into one array whilst making sure that no values are the same.

What would be the best way to go about it?

Perhaps a for loop comparing values for both, creating a third array with the new values?

I'm sure I'm missing something here.

Thanks,

Tris.

Edited by BitByteBit

Share this post


Link to post
Share on other sites



Start out with the first array. Loop through the second array testing each item for duplication in the first. Copy the item to the first array if it's not a duplicate.

:graduated:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the reply,

Something a little like this? Is there a smarter way?

#include<array.au3>
Global $Array1[3][2] = [['1','Bob'],['2','Dave'],['3','Fred']]
Global $Array2[4][2] = [['1','Belinda'],['2','Dorethy'],['3','Fredrika'],['4','Bob']]
Global $TotalArraySize = UBound($Array1,1)
Global $Array3[1][2]
$Array3 = $Array1


For $X = 0 to UBound($Array2,1) - 1
    _ArraySearch($Array1,$Array2[$X][1])
    If @error Then
        $LastElement = UBound($Array3,1) + 1
        ReDim $Array3[$LastElement][2]
        $Array3[$LastElement - 1][1] = $Array2[$X][1]
    EndIf
Next
_ArraySort($Array3,0,0,0,1)
For $X = 0 To UBound($Array3) - 1
    $Array3[$X][0] = $X
Next
_ArrayDisplay($Array3)
Edited by BitByteBit

Share this post


Link to post
Share on other sites

Is there a smarter way?

This is no smarter in that it yeilds the same result, but with large arrays it ought to be a lot faster than all the calls to _ArraySearch()

#include<array.au3>
Global $Array1[3][2] = [['1','Bob'],['2','Dave'],['3','Fred']]
Global $Array2[4][2] = [['1','Belinda'],['2','Dorothy'],['3','Fredrika'],['4','Bob']]

$Idx1 = UBound($Array1)
$Idx2 = UBound($Array2)
$NewIdx = $Idx1 + $Idx2
ReDim $Array1[$NewIdx][2]

For $x = 0 to $Idx2 - 1 ; concatenate arrays
    $Array1[$Idx1 + $x][1] = $Array2[$x][1]
Next
_ArraySort($Array1, 0, 0, 0, 1) ; sort
For $x = $NewIdx - 1 to 1 Step -1 ; delete dupes
    If $Array1[$x][1] = $Array1[$x - 1][1] Then
        _ArrayDelete($Array1, $x)
        $NewIdx -= 1
    EndIf
Next
ReDim $Array1[$NewIdx][2]
For $x = 0 to $NewIdx - 1 ; renumber
    $Array1[$x][0] = $x + 1
Next
_ArrayDisplay($Array1)

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