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.
how to remove elements from an array that are stored in another array
Re: how to remove elements from an array that are stored in another array
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.
- flyingDman
- Posts: 2832
- Joined: 29 Sep 2013, 19:01
Re: how to remove elements from an array that are stored in another array
I don't understand why for k,v in array_2 would be needed twice. I had this:
While elements are being removed Array_1.Count() changes accordingly and i might not behave properly.
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
14.3 & 1.3.7
Re: how to remove elements from an array that are stored in another array
but I do not understand why using traditional Loop
Edit: Silly me, now I know why.....
so I would do something like this
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]
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
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: how to remove elements from an array that are stored in another array
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.
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
Actually, I didn’t mean to have it twice. As I was moving stuff around, somehow I left it in that way.flyingDman wrote: ↑22 Dec 2022, 01:51I don't understand why for k,v in array_2 would be needed twice.
Who is online
Users browsing this forum: No registered users and 198 guests