find previous key in associated array

Get help with using AutoHotkey and its commands and hotkeys
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

find previous key in associated array

05 Dec 2014, 20:17

I want to create an associated array. The keys will be kind of random. They represent time stamps and will timewise in ascending order.
that is no problem so far.

but when I want to know a value at a given time (not necessarily a key) I need to find the "previous" key to get it's value. How do I do hat efficiently?

I do not want to enumerate through the whole array each time and check if the current key is larger and take the previous which was stored. That seems to be very inefficient if it has to be done several thousand times.
ciao
toralf
User avatar
joedf
Posts: 7792
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: find previous key in associated array

05 Dec 2014, 21:11

Maybe if you add an extra index, in a sorted order?
toralf
Posts: 792
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: find previous key in associated array

07 Dec 2014, 21:11

If I'm not mistaken both approaches will not tell me the key that is next to it.

is there a way to access a key of a associated array by an index? If it would, I could create with the time point I have a new key, get his ID, remove it and access the previous key by ID-1.
ciao
toralf
User avatar
joedf
Posts: 7792
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: find previous key in associated array

07 Dec 2014, 21:58

The thing is, that's the point of hotkeyit's code.
All objects iterate in alphanumeric order by default.
That said, one cannot calculate the "previous" of a string since strings can not be in "mathematical" expressions. In example, "hello"-1 == undefined. So having an integer index, you can do ex: 25-1=24. You know that.. Anyway, the easiest way is to manually create an index. Other methods are not worth the effort. Or better yet, in hotkeyit's code, take advantage of the default iteration order.
Coco-guest

Re: find previous key in associated array

07 Dec 2014, 23:59

Something like this?: (obviously Remove() needs to be handled, this is just for demo purposes)

Code: Select all

test := new Dict({a:"A", b:"B", c:"C", d:"d"})
test.apple := "Apple"
test.ahk := "AutoHotkey"
test.carrot := "Carrot"

for k, v in test
{
	prev := test.GetPrevKey(k)
	MsgBox key: %k%`nvalue: %v%`nPrevKey: %prev%
}

class Dict
{
	__New(obj)
	{
		ObjInsert(this, ".", {})
		for k, v in obj
			this[k] := v
	}

	__Set(key, value)
	{
		if !ObjHasKey(this, key)
		{
			ret := ObjInsert(this, key, value)
			this._Update()
			return ret
		}
	}

	Insert(key, value)
	{
		return this[key] := value
	}

	GetPrevKey(key)
	{
		return this["."][key]
	}

	_Update()
	{
		prev := ""
		for k, v in this
			this["."][k] := prev, prev := k
	}

	_NewEnum()
	{
		return { base: { Next: Dict._EnumNext }
		       , enum: ObjNewEnum(this) }
	}

	_EnumNext(ByRef k, ByRef v:="")
	{
		enum := this.enum
		if r := enum.Next(k, v)
		{
			if (k == "." && IsObject(v))
				r := enum.Next(k, v)
		}
		return r
	}
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Dom_Christo, guyzyl, kczx3, kunkel321, mikeyww, Scr1pter, tank, WOlfen and 23 guests