Code: Select all
array := [1,2,3]
array.PushSingle(2)
; array is still [1,2,3]
array.PushSingle(4)
; array is [1,2,3,4]
Code: Select all
array := [1,2,3]
array.PushSingle(2)
; array is still [1,2,3]
array.PushSingle(4)
; array is [1,2,3,4]
Code: Select all
; requires https://www.npmjs.com/package/array.ahk
array := [1,2,3]
value := 2
if (array.indexOf(value) = -1) {
array.push(value)
}
; => [1,2,3]
Code: Select all
; redefine Array() to add custom _Array base object
Array(prms*) {
prms.base := _Array
return prms
}
array := [1,2,3]
value := 2
array.singlePush(2)
; => [1,2,3]
msgbox, % array.Count()
; => 3
; Define the base class.
class _Array {
singlePush(param_value) {
if (this.indexOf(param_value) = -1) {
this.Push("" param_value)
}
}
indexOf(searchElement, fromIndex:=0) {
len := this.Count()
if (fromIndex > 0)
start := fromIndex - 1 ; Include starting index going forward
else if (fromIndex < 0)
start := len + fromIndex ; Count backwards from end
else
start := fromIndex
loop, % len - start
if (this[start + A_Index] = searchElement)
return start + A_Index
return -1
}
}