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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
robo_z
Posts: 6
Joined: 21 Nov 2015, 15:54

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

21 Dec 2022, 21:40

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.
User avatar
boiler
Posts: 17208
Joined: 21 Dec 2014, 02:44

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

21 Dec 2022, 23:34

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.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

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

22 Dec 2022, 01:51

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.
14.3 & 1.3.7
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

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

22 Dec 2022, 03:08

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)
}
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

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

22 Dec 2022, 03:35

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
}

User avatar
boiler
Posts: 17208
Joined: 21 Dec 2014, 02:44

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

22 Dec 2022, 06:14

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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 198 guests