Taking Objects In Order Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
hasantr
Posts: 343
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Taking Objects In Order

21 Jan 2020, 06:33

Code: Select all

Global O := {"15":"40000","13":"50000","9,5":"90000"}
for k, v in O
{
	MsgBox % "v: " v   " k: " k
}
In such a process, objects come not in order, but from small to large.
Is it possible to get it in the order in which it was written? First 15, Then 13, Then 9 ...
User avatar
boiler
Posts: 3468
Joined: 21 Dec 2014, 02:44

Re: Taking Objects In Order

21 Jan 2020, 07:34

For cases where you want to enforce order, you can use an approach like this:

Code: Select all

O_k := ["15","13","9,5"]
O_v := ["40000","50000","90000"]
loop, % O_k.MaxIndex()
	MsgBox % "v: " O_v[A_Index] " k: " O_k[A_Index]
teadrinker
Posts: 1177
Joined: 29 Mar 2015, 09:41
Contact:

Re: Taking Objects In Order  Topic is solved

21 Jan 2020, 09:27

Com object Scripting.Dictionary supports key ordering. I use this wrapper for it:

Code: Select all

obj := new ScriptingDictionary
obj[3] := 1
obj[2] := 2
obj.B  := 3
obj.b  := 4
obj.a  := 5
obj.A  := 6

for k, v in obj
   MsgBox,, Pairs, % k . ": " . v

keys := obj.Keys
for k, v in keys
   MsgBox,, Keys, % v

values := obj.Items
for k, v in values
   MsgBox,, Values, % v

obj.Delete("a")
MsgBox, % obj.HasKey("a")
MsgBox, % obj.HasKey("A")

class ScriptingDictionary
{
   __New() {
      this._dict_ := ComObjCreate("Scripting.Dictionary")
   }
   
   __Delete() {
      this._dict_.RemoveAll()
      this.SetCapacity("_dict_", 0)
      this._dict_ := ""
   }
   
   __Set(key, value) {
      if !(key == "_dict_") {
         if !this._dict_.Exists(key)
            this._dict_.Add(key, value)
         else
            this._dict_.Item(key) := value
         Return value
      }
   }
   
   __Get(key) {
      if (key == "_dict_")
         Return
      if (key == "Keys" || key == "Items") {
         keys := this._dict_[key]
         arr := []
         Loop % this._dict_.Count
            arr.Push(keys[A_Index - 1])
         Return arr
      }
      else
         Return this._dict_.Item(key)
   }
   
   _NewEnum() {
      Return new ScriptingDictionary._CustomEnum_(this._dict_)
   }
   
   class _CustomEnum_
   {
      __New(dict) {
         this.i := -1
         this.dict := dict
         this.keys := this.dict.Keys()
         this.items := this.dict.Items()
      }
      
      Next(ByRef k, ByRef v) {
         if ( ++this.i = this.dict.Count() )
            Return false
         k := this.keys[this.i]
         v := this.items[this.i]
         Return true
      }
   }
   
   Delete(key) {
      if this._dict_.Exists(key) {
         value := this._dict_.Item(key)
         this._dict_.Remove(key)
      }
      Return value
   }
   
   HasKey(key) {
      Return !!this._dict_.Exists(key)
   }
}
guest3456
Posts: 2992
Joined: 09 Oct 2013, 10:31

Re: Taking Objects In Order

21 Jan 2020, 09:35

teadrinker wrote:
21 Jan 2020, 09:27
Com object Scripting.Dictionary supports key ordering. I use this wrapper for it:

Code: Select all

obj := new ScriptingDictionary
obj[3] := 1
obj[2] := 2
obj.B  := 3
obj.b  := 4
obj.a  := 5
obj.A  := 6

for k, v in obj
   MsgBox,, Pairs, % k . ": " . v

keys := obj.Keys
for k, v in keys
   MsgBox,, Keys, % v

values := obj.Items
for k, v in values
   MsgBox,, Values, % v

obj.Delete("a")
MsgBox, % obj.HasKey("a")
MsgBox, % obj.HasKey("A")

class ScriptingDictionary
{
   __New() {
      this._dict_ := ComObjCreate("Scripting.Dictionary")
   }
   
   __Delete() {
      this._dict_.RemoveAll()
      this.SetCapacity("_dict_", 0)
      this._dict_ := ""
   }
   
   __Set(key, value) {
      if !(key == "_dict_") {
         if !this._dict_.Exists(key)
            this._dict_.Add(key, value)
         else
            this._dict_.Item(key) := value
         Return value
      }
   }
   
   __Get(key) {
      if (key == "_dict_")
         Return
      if (key == "Keys" || key == "Items") {
         keys := this._dict_[key]
         arr := []
         Loop % this._dict_.Count
            arr.Push(keys[A_Index - 1])
         Return arr
      }
      else
         Return this._dict_.Item(key)
   }
   
   _NewEnum() {
      Return new ScriptingDictionary._CustomEnum_(this._dict_)
   }
   
   class _CustomEnum_
   {
      __New(dict) {
         this.i := -1
         this.dict := dict
         this.keys := this.dict.Keys()
         this.items := this.dict.Items()
      }
      
      Next(ByRef k, ByRef v) {
         if ( ++this.i = this.dict.Count() )
            Return false
         k := this.keys[this.i]
         v := this.items[this.i]
         Return true
      }
   }
   
   Delete(key) {
      if this._dict_.Exists(key) {
         value := this._dict_.Item(key)
         this._dict_.Remove(key)
      }
      Return value
   }
   
   HasKey(key) {
      Return !!this._dict_.Exists(key)
   }
}
you always end up having good functions and libraries... you should post them in the Scripts & Functions forum so they don't get lost in the suffle of all these Ask For Help questions

teadrinker
Posts: 1177
Joined: 29 Mar 2015, 09:41
Contact:

Re: Taking Objects In Order

21 Jan 2020, 09:45

I'm not sure, maybe someone already wrote something like this. :)
User avatar
boiler
Posts: 3468
Joined: 21 Dec 2014, 02:44

Re: Taking Objects In Order

21 Jan 2020, 09:55

For those interested, here’s a link to the English version of Scripting.Dictionary.
User avatar
boiler
Posts: 3468
Joined: 21 Dec 2014, 02:44

Re: Taking Objects In Order

21 Jan 2020, 10:36

Ha, no problem. Thanks for the link. I was unaware of the Scripting.Dictionary.

Return to “Ask For Help”

Who is online

Users browsing this forum: bwersing, Elgin, garman666, JoeWinograd, markc63 and 280 guests