associative array count?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
panofish
Posts: 11
Joined: 04 Oct 2013, 09:20
Contact:

associative array count?

04 Nov 2013, 14:59

I am using autohotkey_L and I have an associative array where I have created 2 key/value pairs like this:

Code: Select all

hash := object()   ; associative hash
hash["alan"] := 1
hash["tom"] := 1

Is there a method or something so that I get the count of values in the associative array?
In the above example I expect 2.

count() and maxindex() don't work.
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: associative array count?

04 Nov 2013, 15:02

Store the last A_Index of an enumeration (for-loop).
User avatar
panofish
Posts: 11
Joined: 04 Oct 2013, 09:20
Contact:

Re: associative array count?

04 Nov 2013, 15:06

jethrow wrote:Store the last A_Index of an enumeration (for-loop).
I was hoping to avoid that. :)
Any other suggestions?
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: associative array count?

04 Nov 2013, 15:07

No - other than proposing that feature, hacking the internal structure of the object, or just storing the item count yourself.
User avatar
panofish
Posts: 11
Joined: 04 Oct 2013, 09:20
Contact:

Re: associative array count?

04 Nov 2013, 15:12

Thanks jethrow... much appreciated. :)
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: associative array count?

04 Nov 2013, 22:58

This might help:

Code: Select all

d := dict("z", "Hello World", "y", "AutoHotkey", "x", "Some string")
d["w"] := "Another string"

out := "Total count: " d.len() ; use the len() method to get the total number of items
for k, v in d
	out .= "`n" . k " = " v
MsgBox, % out
return

dict(p*) {
	return new __dict__(p*)
}

class __dict__
{
	__New(p*) {
		ObjInsert(this, "_", []) ; keys
		for k, v in p {
			if !Mod(A_Index, 2)
				this[key] := v
			else key := v	
		}
	}

	__Set(k, v, p*) {
		this._.Insert(k)
	}

	__Get(k, p*) {
		
	}

	len() {
		return this._.MaxIndex()
	}

	_NewEnum() {
		return new __dict__.Enum(this)
	}

	class Enum {
		__New(obj) {
			this.obj := obj
			this.enum := obj._._NewEnum()
		}

		Next(ByRef k, ByRef v:="") {
			if (r:=this.enum.Next(i, k))
				v := this.obj[K]
			return r
		}
	}
}
lexikos
Posts: 8292
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: associative array count?

05 Nov 2013, 00:23

Object.SetCapacity(0) returns the number of key-value pairs in the object, but only after trimming off any unused space. (It could be slow and/or cause more memory fragmentation.)
Creeall_Anonymously

Re: associative array count?

05 Nov 2013, 03:19

Why not just
[code]MaxIndex()[/code]

hm ?
Creeall_Anonymously

Re: associative array count?

05 Nov 2013, 03:22

Hm maybe not for associative array after all...
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: associative array count?

05 Nov 2013, 10:11

More info.

Return to “Ask For Help”

Who is online

Users browsing this forum: arczi_87, Boss55, Chunjee, Green Astronaut, hitman, inseption86, one1tick, XMCQCX and 61 guests