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.
find previous key in associated array
find previous key in associated array
ciao
toralf
toralf
Re: find previous key in associated array
Maybe if you add an extra index, in a sorted order?
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: find previous key in associated array
You could save it in obj[""]:={(key):value}.
Re: find previous key in associated array
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.
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
toralf
Re: find previous key in associated array
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.
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.
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: find previous key in associated array
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
}
}
Who is online
Users browsing this forum: Google [Bot], yabab33299 and 139 guests