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.
Removing odd-numbered keys from an array Topic is solved
Re: Removing odd-numbered keys from an array
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%#
Re: Removing odd-numbered keys from an array
Wow, that was a super fast response and the solution works PERFECTLY.
Thank you very much for your help.
Thank you very much for your help.
- AlphaBravo
- Posts: 586
- Joined: 29 Sep 2013, 22:59
Re: Removing odd-numbered keys from an array Topic is solved
@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:
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
Re: Removing odd-numbered keys from an array
You're right.
Re: Removing odd-numbered keys from an array
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
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
Re: Removing odd-numbered keys from an array
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.
Re: Removing odd-numbered keys from an array
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
}
}
Re: Removing odd-numbered keys from an array
Nice! Thank you for sharing this, @Chunjee.
Who is online
Users browsing this forum: No registered users and 325 guests