[FUNC]AHK 判断变量类型,区分数组和关联数组

    xiaoxiao2023-10-16  28

     

    arr:=["hello","world"] ;数组元素不能同时包含字符串和纯数字。 MsgBox % type(arr) MsgBox % ObjGetCapacity(arr) " " ObjLength(arr) " " ObjCount(arr) ;ObjGetCapacity返回对象或其一个字段当前占用的内存空间.ObjLength(brr) 对象中整数键的最大位置 ;ObjCount 返回对象中存在的键-值对的数量 brr:={ten: 10, twenty: 20, thirty: 30} MsgBox % type(brr) MsgBox % ObjGetCapacity(brr) " " ObjLength(brr) " " ObjCount(brr) ;~ MsgBox % type("") ; String ;~ MsgBox % type(1) ; Integer ;~ MsgBox % type(1/1) ; Float ;~ MsgBox % type("1") ; String ;~ MsgBox % type(2**42) ; Integer return type(v) { if IsObject(v) { if ObjGetCapacity(v)>ObjCount(v) return "associative array" else return "array" } return v="" || [v].GetCapacity(1) ? "String" : InStr(v,".") ? "Float" : "Integer" } ;~ This relies on the following: ;~ Unlike a variable, an array element can't contain a string and a pure number simultaneously. ;~ A field's "capacity" is the size of string it can hold before it needs to expand. If it doesn't hold a string it has no capacity. ;~ SetFormat hasn't been used to remove the decimal point from the floating-point string format. (If var is float has the same limitation, since it always converts the value to a string for analysis.)

     

    最新回复(0)