Page 1 of 1

how to remove elements from an array that are stored in another array

Posted: 21 Dec 2022, 21:40
by robo_z
I want to delete elements of a first array that are in a second array, leaving only the elements that are not in the second array, for example

Before

Array_1 := ["Juan", "Leo", "Maria", "Jose", "Luis", "Pedro"]
Array_2 := ["Juan", "Maria", "Luis"]

After

Array_1 := ["Leo", "Jose", "Pedro"]

thanks for you help.

Re: how to remove elements from an array that are stored in another array

Posted: 21 Dec 2022, 23:34
by boiler

Code: Select all

Array_1 := ["Juan", "Leo", "Maria", "Jose", "Luis", "Pedro"]
Array_2 := ["Juan", "Maria", "Luis"]

for k, v in Array_2
	loop, % Array_1.Count() {
		i := Array_1.Count() - A_Index + 1
		for k, v in Array_2
			if (Array_1[i] = v)
				Array_1.RemoveAt(i)
	}
	
;display result:
for k, v in Array_1
	out .= v "`n"
MsgBox, % out

Steps through Array_1 backwards in the inner loop so that it doesn't upset the keys of the remaining items when one is removed.

Re: how to remove elements from an array that are stored in another array

Posted: 22 Dec 2022, 01:51
by flyingDman
I don't understand why for k,v in array_2 would be needed twice. I had this:

Code: Select all

Array_1 := ["Juan", "Leo", "Maria", "Jose", "Luis", "Pedro"]
Array_2 := ["Juan", "Maria", "Luis"]

loop, % cnt := Array_1.count()              ; cnt should not vary
	{
	i := cnt - A_Index + 1                  ; this is not the same as i := Array_1.Count() - A_Index + 1
	for k,v in array_2
		if (Array_1[i] = v)
			Array_1.RemoveAt(i)
	}
;display result:
for k, v in Array_1
	out .= v "`n"
MsgBox, % out
While elements are being removed Array_1.Count() changes accordingly and i might not behave properly.

Re: how to remove elements from an array that are stored in another array

Posted: 22 Dec 2022, 03:08
by Xeo786
but I do not understand why using traditional Loop

Code: Select all

Array_1 := ["Juan", "Leo", "Maria", "Jose", "Luis", "Pedro"]
Array_2 := ["Juan", "Maria", "Luis"]
for k, v in Array_1
	for i, r in array_2
		if(v = r)
			Array_1.RemoveAt(k)
msgbox, % Array_1[1] "`n" Array_1[2] "`n" Array_1[3]
Edit: Silly me, now I know why..... :lolno:

so I would do something like this

Code: Select all

Array_1 := ["Juan", "Leo", "Maria", "Jose", "Luis", "Pedro"]
Array_2 := ["Juan", "Maria", "Luis","Leo"]
for k, v in array_2
	removefromarray(Array_1,v)
msgbox, % Array_1[1] "`n" Array_1[2] "`n" Array_1[3]
return

removefromarray(byref array,value)
{
	for k, v in array
		if(v = value)
			array.RemoveAt(k)
}

Re: how to remove elements from an array that are stored in another array

Posted: 22 Dec 2022, 03:35
by AHKStudent
Here is a way to do it using the hasVal function by @jNizM

Obviously if you are trying to do this with a lot of data, this might not be the best way to do.

Code: Select all

Array_1 := ["Juan", "Leo", "Maria", "Jose", "Luis", "Pedro"]
Array_2 := ["Juan", "Maria", "Luis"]
for k, v in Array_2
{
	if x := HasVal(Array_1, v)
		Array_1.RemoveAt(x)
}
;display result:
for k, v in Array_1
	out .= v "`n"
MsgBox, % out
ExitApp

HasVal(haystack, needle) {
    for index, value in haystack
        if (value = needle)
            return index
    if !(IsObject(haystack))
        throw Exception("Bad haystack!", -1, haystack)
    return 0
}


Re: how to remove elements from an array that are stored in another array

Posted: 22 Dec 2022, 06:14
by boiler
flyingDman wrote:
22 Dec 2022, 01:51
I don't understand why for k,v in array_2 would be needed twice.
Actually, I didn’t mean to have it twice. As I was moving stuff around, somehow I left it in that way.