Code: Select all
getType([1,2]) ;return "Array"
Code: Select all
getType({"a":"aa", "b","bb"}) ;return "Object"
Code: Select all
getType([1,2]) ;return "Array"
Code: Select all
getType({"a":"aa", "b","bb"}) ;return "Object"
Code: Select all
;requires AHK v1.1.29+
;has integer keys 1 to n with no gaps, or is an empty object
IsArray(oArray)
{
local
if !ObjCount(oArray)
return 1
if !(ObjCount(oArray) = ObjLength(oArray))
|| !(ObjMinIndex(oArray) = 1)
return 0
for vKey in oArray
if !(vKey = A_Index)
return 0
return 1
}
Code: Select all
oArray := ["a", "b", "c"]
MsgBox, % oArray.Length() " " oArray.Count() ;3 3
oArray := {1:"a", 3:"c", x:"x"}
MsgBox, % oArray.Length() " " oArray.Count() ;3 3
Code: Select all
isArray(arrOrObj) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64332
return arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == arrOrObj.Count()
}
Code: Select all
isArray(arrOrObj) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64332
return !ObjCount(arrOrObj) || ObjMinIndex(arrOrObj) == 1 && ObjMaxIndex(arrOrObj) == ObjCount(arrOrObj) && arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == ObjCount(arrOrObj)
}
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines, -1
#KeyHistory 0
ListLines Off
Frequency := 0
Before := 0
After := 0
DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
DllCall("QueryPerformanceCounter", "Int64*", Before)
; dontModifyThis:={1:"a", 2:"b", "3":"c", 5:"x", x:"5"}
dontModifyThis:={x:"5"}
loop 10000 {
dontModifyThis.Push(A_Index)
}
; dontModifyThis[dontModifyThis.Count() + 1]:=true
; d(ObjCount(dontModifyThis) ", " ObjLength(dontModifyThis))
; MsgBox % isItArray2(dontModifyThis)
QPC(1)
sleep, 1000 ;0.988971
testN := QPC(0), QPC(1)
loop 10000 {
if (JEE_IsArray(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
testJEE := QPC(0), QPC(1)
loop 10000 {
if (isItArray1(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
test1 := QPC(0), QPC(1)
loop 10000 {
if (isItArray2(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
test2 := QPC(0), QPC(1)
loop 10000 {
if (isItArray3(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
test3 := QPC(0), QPC(1)
loop 10000 {
if (isItArray4(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
test4 := QPC(0), QPC(1)
loop 10000 {
if (isItArray5(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
test5 := QPC(0), QPC(1)
loop 10000 {
if (isItArray6(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
test6 := QPC(0), QPC(1)
loop 10000 {
if (isItArrayCorrect(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
testCorrect := QPC(0), QPC(1)
loop 10000 {
if (isItArrayObjF(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
testObjF := QPC(0), QPC(1)
loop 10000 {
if (isItArrayObjFo(dontModifyThis)!=0) {
MsgBox "Error 2"
}
}
testObjFo := QPC(0), QPC(1)
d([testJEE, test1, test2, testObjF, testObjFo, test3, test4, test5, test6, testCorrect, testN])
d(dontModifyThis)
; 0.010993
; 0.445327
; 0.290402
; 0.281568
; 0.290992
; 0.009780
; 0.008286
; 0.008505
; 0.008501
; 0.009243
; 0.986390
Exitapp
isItArray1(arrOrObj) {
return arrOrObj.Clone().Delete(arrOrObj.MinIndex(), arrOrObj.MaxIndex()) == arrOrObj.Count()
}
isItArray2(arrOrObj) {
return arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == arrOrObj.Count()
}
isItArrayObjF(arrOrObj) {
return ObjDelete(ObjClone(arrOrObj), 1, arrOrObj.MaxIndex()) == ObjCount(arrOrObj)
}
isItArrayObjFo(arrOrObj) {
return arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == ObjCount(arrOrObj)
}
isItArray3(arrOrObj) {
return arrOrObj.MinIndex() == 1 && arrOrObj.MaxIndex() == arrOrObj.Count() && arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == arrOrObj.Count()
}
isItArray4(arrOrObj) { ;fastest
return ObjMinIndex(arrOrObj) == 1 && ObjMaxIndex(arrOrObj) == ObjCount(arrOrObj) && arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == ObjCount(arrOrObj)
}
isItArray5(arrOrObj) {
return ObjMinIndex(arrOrObj) = 1 && ObjMaxIndex(arrOrObj) = ObjCount(arrOrObj) && arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) = ObjCount(arrOrObj)
}
isItArray6(arrOrObj) {
return ObjMinIndex(arrOrObj) == 1 && ObjMaxIndex(arrOrObj) == ObjCount(arrOrObj) && arrOrObj.Clone().Delete(1, ObjMaxIndex(arrOrObj)) == ObjCount(arrOrObj)
}
isItArrayCorrect(arrOrObj) {
return !ObjCount(arrOrObj) || ObjMinIndex(arrOrObj) == 1 && ObjMaxIndex(arrOrObj) == ObjCount(arrOrObj) && arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == ObjCount(arrOrObj)
}
JEE_IsArray(oArray)
{
local
if !ObjCount(oArray)
return 1
if !(ObjCount(oArray) = ObjLength(oArray))
|| !(ObjMinIndex(oArray) = 1)
return 0
for vKey in oArray
if !(vKey = A_Index)
return 0
return 1
}
QPC(R := 0)
{
static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", F)
return ! DllCall("QueryPerformanceCounter", "Int64P", Q) + (R ? (P := Q) / F : (Q - P) / F)
}
f3::Exitapp
Code: Select all
assert(isArray([]), 1) ;1
assert(isArray([1,2,3]), 1) ;1
assert(isArray({1:1, 3:3, 4:4}), 0) ;0
assert(isArray({1:"a", 3:"c", x:"x"}), 0) ;0
assert(isArray({1:"a", "2":"b", 3:"c"}), 0) ;0
assert(isArray([1,2,3]), 0) ;1 this should throw, to test my assert function :)
isArray(arrOrObj) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64332
return !ObjCount(arrOrObj) || ObjMinIndex(arrOrObj) == 1 && ObjMaxIndex(arrOrObj) == ObjCount(arrOrObj) && arrOrObj.Clone().Delete(1, arrOrObj.MaxIndex()) == ObjCount(arrOrObj)
}
assert(actual, expected) {
if !(actual==expected)
MsgBox % "Line " Exception("", -1).Line " Failed`nactual: " actual "`nexpected: " expected
;https://www.autohotkey.com/board/topic/71487-can-a-function-grab-the-calling-lines-line-number/#post_id_453358
}