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

 


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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值