Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Ordered Array


  • Please log in to reply
9 replies to this topic
rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Normally, looping through the keys in an associative array will present them in alphabetical order as in this script:
Array := Object("b", "1st", "a", "2nd")
For Key, Value in Array
    MsgBox %Key%, %Value%
For some applications, however, it is important that the keys be presented in the order that they were created. I the script above, for instance, b, 1st was probably wanted before a, 2nd. Searching the forums, I found this post buried in the support section from two years ago.

I took up Lexikos' challenge and added the .Insert(), .InsertBefore() and .Remove() methods. I also changed the __set method to allow the array to be sub-classed if necessary.

Examples assume OrderedArray() is in one of your Standard Libraries.

The function:
Spoiler

Example Code:
Spoiler

Example of InsertBefore()
Spoiler

Edited by rbrtryn, 19 August 2013 - 01:32 AM.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


ahkkha
  • Members
  • 4 posts
  • Last active: Jan 21 2014 01:37 PM
  • Joined: 30 Oct 2012

Thanks for this great job. AHK should have native support for it.



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
New feature: Added an InsertBefore() method, which allows items to be inserted at a specific location in the array. Added an example of how this works in the OP.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Moebius
  • Members
  • 39 posts
  • Last active: May 11 2015 08:46 PM
  • Joined: 08 Mar 2009

There is a problem, if an alphanumeric key is used with Insert():

afp := OrderedArray()
afp.Insert("a","fdsfsf")
for k,v in afp 
msgbox %k% -- %v%

returns 2 items:

_keys

a -- fdsfsf



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

There is a problem, if an alphanumeric key is used with Insert():
returns 2 items:
_keys
a -- fdsfsf


Thanks for reporting that. It should be fixed now. Try the new version and let me know if the are any further problems.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Moebius
  • Members
  • 39 posts
  • Last active: May 11 2015 08:46 PM
  • Joined: 08 Mar 2009

Thanks for the quick response.

Meanwhile another problem occured:

 

I use the array to store window handles of controls. The key is the hwnd (hex) and after the first item, an insert generates more than one entry.

afp := OrderedArray()
afp.Insert(0x7880696,"fdsfsf")
afp.Insert(0x62d14f8,"vcvcxv")
afp.Insert(0x1900c60,"xcvcx")
for k,v in afp 
msgbox %k% -- %v%

Result:

126355094--
103617784--
126355095--
26217568--xcvcx
103617785--vcvcxv
126355096--fdsfsf
 

The logic looks like this:

1. ok

2. + 1 item with key - 1

3. + 2 items with key -2 and -1



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

I use the array to store window handles of controls. The key is the hwnd (hex) and after the first item, an insert generates more than one entry.


This is caused by the way AutoHotkey internally handles the Insert() method. For integer keys, Insert() will automatically increment any integer keys greater than one you are inserting. An example with a standard array:
afp := []
afp.Insert(0x7880696,"fdsfsf")
afp.Insert(0x62d14f8,"vcvcxv")
afp.Insert(0x1900c60,"xcvcx")
for k,v in afp 
msgbox %k% -- %v%
return
The solution is don't use Insert():
afp := OrderedArray()
afp[0x7880696] := "fdsfsf"
afp[0x62d14f8] := "vcvcxv"
afp[0x1900c60] := "xcvcx"
for k,v in afp 
msgbox %k% -- %v%
The doubled output of keys you are seeing is because my oaInsert() method has no way of knowing which keys were incremented and which were not, so the _keys array includes both versions.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Just to be clear, is the following the same as the issue that Moebius had?

{
#Include <OrderedArray>

	t := ""
	Array := OrderedArray()
	Array[4, "four"] := 1
	Array[3, "three"] := 2
	Array[1, "one"] := 3
	Array[2, "two"] := 4

	for sec, aData in Array
		for k, v in aData
			t .= k " = " v "`n"
	MsgBox % t

	return
}

Output:

---------------------------
AutoHotkey.ahk
---------------------------
three = 3
four = 4

---------------------------
OK   
---------------------------
 


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


Larctic
  • Members
  • 303 posts
  • Last active: May 10 2016 04:56 PM
  • Joined: 21 Jul 2012

Will the availability of V2 version?



SnowFlake_FlowSnake
  • Members
  • 845 posts
  • Last active: Jan 24 2016 05:24 PM
  • Joined: 08 Oct 2012

Will the availability of V2 version?

1+ for that :D


  • Download link of my scripts on Autohotkey.com 2/10/2015 [DOWNLAND]
  • Contact Info:  https://github.com/floowsnaake //  FloowSnaake(A)gmail.com
  • IF you need Help send me a PM,Email or Post on Github

  • Quote by tank  Posted 29 September 2015 - 06:14 PM

  • "Eventually i will find a way to convert the DB back to PHPBB3. but i dont have the bandwidth right now. No one that has tried has had success. It is the Only way i can keep this open is if i could successfully convert it."