Another topic from 2011 with some requests (which also show I'm not the only one who wants/needs these) http://www.autohotkey.com/board/topic/7 ... min/page-2
wish list:
.count() ; this is what I'd expect .length() to do, but since it doesn't, .count() could exist aswell
counts the number of items in an array or object, regardless of type.Code: Select all
z:={"aaa":"bbb","ccc":"ddd","yyy":"zzz"} a:={"aaa":"bbb","ccc":"ddd",111:222} b:={1:2,3:4,9:10} c:=[1,2,3,4,20] d:=["a","b","c","d","e"] ; c=count for k in z cz++ for k in a ca++ for k in b cb++ for k in c cc++ for k in d cd++ msgbox, % "Maxi() `t len() `t Expected # `t for-loop" . "`n" z.maxIndex() " `t " z.length() " `t Expected: 3 `t " cz . "`n" a.maxIndex() " `t " a.length() " `t Expected: 3 `t " ca . "`n" b.maxIndex() " `t " b.length() " `t Expected: 3 `t " cb . "`n" c.maxIndex() " `t " c.length() " `t Expected: 5 `t " cc . "`n" d.maxIndex() " `t " d.length() " `t Expected: 5 `t " cd . "`n`nI'd expect .length() to return the # of items in an object OR array, no matter the contents. A for-loop should not be needed for such a common task. You should be able to use it inline any function or expression."
contains(mixed, list*) ; found in String Things ( http://ahkscript.org/boards/viewtopic.p ... 3&start=20 )
does an array/object/variable contain any of the specified values? return 1!Code: Select all
st_contains(mixed, lookFor*) { if (IsObject(mixed)) { for key1,input in mixed for key,search in lookFor if (InStr(input, search)) Return 1 } Else { for key,search in lookFor if (InStr(mixed, search)) Return 1 } Return 0 }
.find(value)
does an array/object have this value? return the key name/number!
.clone()
(aka: deep copy, deep clone, full clone, various other names)
many people, people such as
uname: http://www.autohotkey.com/board/topic/1 ... /?p=638500
GeekDude: http://www.autohotkey.com/board/topic/8 ... -and-more/
various others and myself,
have made functions for this. IMO, this is the behaviour I and many other expect clone() to do.
Just a function to make a quick copy or backup of an array/object without it ever being modified by its parent or it modifying its parent. A completely different thing.
printarr(arr, depth=5) ; found in String Things ( http://ahkscript.org/boards/viewtopic.p ... 3&start=20 )
return the structure of an array/object, up to N dimensions, as a string.
I use this SO MUCH. It's so handy for debugging.Code: Select all
st_printArr(array, depth=5, indentLevel="") { for k,v in Array { list.= indentLevel "[" k "]" if (IsObject(v) && depth>1) list.="`n" st_printArr(v, depth-1, indentLevel . " ") Else list.=" => " v list.="`n" } return rtrim(list) }
example:Code: Select all
; this version is by AfterLemon is shorter and better. But it's also pretty buggy. ; It also looks nicer. However, I keep both versions available since it's quite ; hard to read :D st_printArr(array,depth:=10,indentLevel:=" ") { static parent,pArr,depthP=depth For k,v in (Array,(!IsObject(pArr)?pArr:=[]:"")) { Loop,% ((d2=depth&&dP2=depthP)?0:(depth-depthP+1,d2:=depth)) parent:=SubStr(a:=SubStr(parent,1,InStr(parent,",",0,0)-1),1,InStr(a,",",0,0)),dP2:=depthP++ k:=RegExReplace(k,","),list.=(indentLevel "arr[" pArr[depth]:=parent (k&1=""?"""" k """":k) "]"),((IsObject(v)&&depth>1)?(parent.=k ",",list.="`n" st_printArr(v,(depthP:=depth)-1,indentLevel " ")):list.=" = " v),list.="`n" }return RTrim(list,"`n") }
msgbox % st_printArr([11,{"key":"val","key2":"val2"}, 22,33]) ; using Afterlemons
ouputs something like:Code: Select all
arr[1] = 11 arr[2] arr[2,"key"] = val arr[2,"key2"] = val2 arr[3] = 22 arr[4] = 33
Thanks for readingObject.Key(N) / Array.Key(N)
Retrieve the specified key name, without a for-loop or confusing _NewEnum().Next().
If it's an array, you might have values with gaps between array indexes (not 1,2,3,4 but 1,3,23,75), however, that doesn't make the array 75 items long. It's 4 items with a maxIndex of 75. Doing arr.key(4) would return 75 because 75 is the index/key name of the 4th item.Code: Select all
stuff:={"aaa":"bbb", "111":"222"} arr:=[], arr[1]:=111, arr[2]:="xxx", arr[23]:=333, arr[75]:="zzz" stuff._NewEnum().next(key,val) msgBox % key ; but how could I get "aaa"? ; What I would like: ; Get the keys name of the specified item. ; msgBox % stuff.key(1) ; --> aaa ; msgBox % stuff.key(2) ; --> 111 ; msgBox % arr.key(4) ; --> zzz