Code: Select all
Array := [1,0,1,1,0,1,0,0,0,1,0]
Thanks
Code: Select all
Array := [1,0,1,1,0,1,0,0,0,1,0]
Code: Select all
Array := [1,0,2,3,0,4,0,0,0,5,0]
ArrRemoveZero(Array)
;-------------------------------------------------------------------------------
ArrRemoveZero(ByRef arr) { ; return a array without zeroes
;-------------------------------------------------------------------------------
loop, % Len := arr.Length()
if (arr[i := Len - A_Index + 1] = 0)
arr.RemoveAt(i)
}
Code: Select all
q:: ;remove empty keys
Array := [1,0,1,1,0,1,0,0,0,1,0]
Loop, % vNum := Array.Length()
{
if !Array[vNum]
Array.RemoveAt(vNum)
vNum--
}
MsgBox, % JEE_ObjList(Array)
return
w:: ;remove empty keys (shorter but less readable)
Array := [1,0,1,1,0,1,0,0,0,1,0]
Loop, % vNum := Array.Length()
if !Array[vNum--]
Array.RemoveAt(vNum+1)
MsgBox, % JEE_ObjList(Array)
return
Code: Select all
Array := [1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0]
Ones := []
Ones.SetCapacity(Array.GetCapacity())
for k, v in Array
if v
Ones.Push(v)
Array := Ones
Ones := ""
But I didn't want to change the name of the array, I just wanted to remove elements from the already existing array.
Code: Select all
Array := [1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0]
Array := remove_zeros(Array)
remove_zeros(Array) {
if !IsObject(Array)
throw "Invalid argument. Not an object."
Ones := []
Ones.SetCapacity(Array.GetCapacity())
for k, v in Array
if v
Ones.Push(v)
return Ones
}
Code: Select all
#NoEnv
#KeyHistory 0
SetBatchLines -1
ListLines Off
tick() {
DllCall("QueryPerformanceCounter", "Int64*", tick)
return tick
}
rand(a, b) {
Random r, a, b
return r
}
make_arrays(size, ByRef Mixed, ByRef MixedClone, ByRef Ones) {
if !IsObject(Mixed)
Mixed := []
Mixed.SetCapacity(size)
Loop % size - Mixed.Count()
Mixed.Push(rand(0, 1))
MixedClone := Mixed.Clone()
Ones := []
Ones.SetCapacity(size)
}
tick_removeat(Array) {
start := tick()
Loop, % vNum := Array.Length()
if !Array[vNum--]
Array.RemoveAt(vNum+1)
return tick() - start
}
tick_push(Mixed, Ones) {
start := tick()
for k, v in Mixed
if v
Ones.Push(v)
return tick() - start
}
csv := "numElem,.RemoveAt(),.Push()`n"
for k, numElements in [1, 10, 100, 1000, 10000, 100000, 1000000]
{
make_arrays(numElements, Mixed, MixedClone, Ones)
csv .= numElements "," tick_removeat(MixedClone) "," tick_push(Mixed, Ones) "`n"
}
MsgBox % Clipboard := csv
Code: Select all
numElem | .RemoveAt() | .Push()
-------------------------------
1 | 38 | 32
10 | 55 | 34
100 | 308 | 192
1000 | 4060 | 1928
10000 | 213709 | 19687
100000 | 25565595 | 204756
1000000 | 7728849218 | 2106586
Code: Select all
Array := [1, 0, 2, 3, 0, 4, 0, 0, 0, 5, 0]
For i, v in Array
out .= v ","
Array := StrSplit(Trim(RegExReplace(out,"\b0,"),","),",")
Code: Select all
startTime := A_TickCount
Loop 10000000 ; Ten_Million
{
myArray := [1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0] ; 30 sec
Loop, % myMax := myArray.Length()
If !myArray[myMax --]
myArray.RemoveAt(myMax + 1)
}
stopTime1 := RegExReplace(Floor((A_TickCount - startTime)/1000), "(?<=\d)(?=(\d{3})+$)", ",") " sec"
startTime := A_TickCount
Loop 10000000
{
myArray := [1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0] ; 25 sec
myArray2 := []
myArray2.SetCapacity(myArray.GetCapacity())
For Each, x In myArray
If x
myArray2.Push(x)
myArray := myArray2
myArray2 := ""
}
stopTime2 := RegExReplace(Floor((A_TickCount - startTime)/1000), "(?<=\d)(?=(\d{3})+$)", ",") " sec"
MsgBox % stopTime1 "`n" stopTime2
Would you be willing to share JEE_ObjList.. I have searched forum / web and had no luck. Thank you very much.Code: Select all
MsgBox, % JEE_ObjList(Array)
pneumatic wrote: ↑28 Mar 2019, 18:38What is the most efficient way to remove all the 0's from this array, leaving no "blank" elements?Code: Select all
Array := [1,0,1,1,0,1,0,0,0,1,0]
Code: Select all
A := new biga() ; requires https://www.npmjs.com/package/biga.ahk
Array := [1,0,1,1,0,1,0,0,0,1,0]
NewArray := A.compact(Array)
msgbox, % A.print(NewArray)
; => [1, 1, 1, 1, 1]