Removing odd-numbered keys from an array Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
leehere10
Posts: 10
Joined: 23 Mar 2018, 04:02

Removing odd-numbered keys from an array

27 Oct 2020, 09:30

Hi,

I wonder if someone could help me out with the best way to approach this task.

I have a simple object array of unknown length, e.g.

Array := [Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9, Item10, Item11, Item12, Item13]

All the odd-numbered keys are complete garbage and I'd like to remove them from the array.

The output should be an array that only includes the even numbered keys from the original array.

Therefore, the output should be an array that contains these values:

Array := [Item2, Item4, Item6, Item8, Item10, Item12]

What would be the best approach for this?

1. Build a second array from the first array and delete the original.

2. Somehow delete the odd-numbered keys in a loop. I'm not sure that would work as the keys would shift after each iteration.

Many thanks for any pointers or help in achieving this. :)
User avatar
mikeyww
Posts: 4191
Joined: 09 Sep 2014, 18:38

Re: Removing odd-numbered keys from an array

27 Oct 2020, 09:40

Code: Select all

Array := ["Item1", "Item2", "Item3", "Item4", "Item5", "Item6"
 , "Item7", "Item8", "Item9", "Item10", "Item11", "Item12", "Item13"]
Loop
 Array.RemoveAt(A_Index)
Until !Array[A_Index + 1]
Loop, % Array.Count()
 ttext .= "`n" Array[A_Index]
ttext := SubStr(ttext, 2)
MsgBox, #%ttext%#
leehere10
Posts: 10
Joined: 23 Mar 2018, 04:02

Re: Removing odd-numbered keys from an array

27 Oct 2020, 09:47

Wow, that was a super fast response and the solution works PERFECTLY.

Thank you very much for your help. :)
User avatar
AlphaBravo
Posts: 503
Joined: 29 Sep 2013, 22:59

Re: Removing odd-numbered keys from an array  Topic is solved

27 Oct 2020, 09:54

@mikeyww, I see a possible flaw using Until !Array[A_Index + 1] in case one of the odd array values is set to 0
here is my solution:

Code: Select all

Array := ["Item1", "Item2", "Item3", "Item4", 0, "Item6", "Item7", "Item8", "Item9", "Item10", "Item11", "Item12", "Item13"]
Num := Ceil(Array.count()/2)
loop, % num
	Array.RemoveAt(A_Index)
for k, v in Array
	res .= "Array " k " = " v "`n"
MsgBox % res
leehere10
Posts: 10
Joined: 23 Mar 2018, 04:02

Re: Removing odd-numbered keys from an array

27 Oct 2020, 10:24

Thank you for the additional solution. I've tested it quickly and it does appear that a 'zero' throws a spanner in the works. Your solution works well.

In my case the values are all long strings with a mixture of numbers and letters, so maybe it wouldn't present a problem, but I'm still learning to program so I'm not sure...

Anyway, thank you both for taking the time to work that through. Most appreciated :)
User avatar
mikeyww
Posts: 4191
Joined: 09 Sep 2014, 18:38

Re: Removing odd-numbered keys from an array

27 Oct 2020, 10:38

The methods are similar except the noted difference about 0. The second method computes in advance the number of iterations, which is a known number based on the array size.
User avatar
Chunjee
Posts: 754
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Removing odd-numbered keys from an array

28 Oct 2020, 15:51

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

Array := ["Item1", "Item2", "Item3", "Item4", "Item5", "Item6"
 , "Item7", "Item8", "Item9", "Item10", "Item11", "Item12", "Item13"]
evenOnlyArray := A.filter(Array, Func("fn_filterEven"))
; => ["Item2", "Item4", "Item6", "Item8", "Item10", "Item12"]

fn_filterEven(param_iteratee, param_key) {
	if (mod(param_key, 2) = 0) {
		return true
	}
}
User avatar
mikeyww
Posts: 4191
Joined: 09 Sep 2014, 18:38

Re: Removing odd-numbered keys from an array

28 Oct 2020, 15:53

Nice! Thank you for sharing this, @Chunjee.

Return to “Ask For Help”

Who is online

Users browsing this forum: Freshman, MarcelloGammer, martin536, pythonker and 36 guests