AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AHKArray [Real Array - One Variable] [Version 5]
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
olegbl



Joined: 13 Dec 2006
Posts: 19

PostPosted: Wed Dec 13, 2006 7:26 am    Post subject: AHKArray [Real Array - One Variable] [Version 5] Reply with quote

Downloads
Latest Version : 5.42 (2008:0502:1620)
Download (v5.42) @ Location 1 : AHKArray AHK File
Download (v5.41) @ Location 1 : AHKArray AHK File
Download (v5.39) @ Location 1 : AHKArray AHK File
Download (v5.38) @ Location 1 : AHKArray AHK File
Download (v5.37) @ Location 1 : AHKArray AHK File
Download (v5.23) @ Location 1 : AHKArray AHK File

Bug Reports
Please send all bug reports, comments, suggestions, etc to olegbl@gmail.com,
or post them as replies to this forum topic.
PS. Feel free to edit/use code as you like, the license just says that you're
allowed to do anything you like, as long as the license stays there, and my
name doesn't get used for advertising (yeah right XD).

Description
AHKA stands for AutoHotkey Array
AHKArray provides a way to store multiple variables, as one. It eliminates the necessity for using global variable arrays that are generated by functions such a stringsplit.
Meaning, arrays can be stored in ONE varaible, and returned by a function. No more array variable clutter.
AHKArray supports an "infinite" amount of dimensions. (Well, as much as your memory can take without blowing up.)
Starting with v5.35, contents of Array are HEXed, enabling the use of ANY of the first 256 ASCII characters inside them.

Specifics
~ Syntax : For Non-HEXed AHKArray is similar to Macromedia (now Adobe) Flash
Code:
Array := "[a,b,[A,B],c]"

~ Syntax : For HEXed AHKArray is similar to a HEXed Macromedia (now Adobe) Flash
Code:
Array := "[61,62,[97,98],63]"

~ Compatability : Conversion from standard Array%i% arrays, is inbuilt
~ Add/Get/Remove/Set advanced functions :
    Index=0 : (DEFAULT) Add/Get/Remove/Set the LAST index in the array
    Code:
    AHKAAdd("[1,2,3]",BLAH) ;returns "[1,2,3,BLAH]" ;same as AHKAAdd("[1,2,3]",BLAH,0)

    Index>0 : Add/Get/Remove/Set the INDEX index in the array
    Code:
    AHKAAdd("[1,2,3]",BLAH,2) ;returns "[1,BLAH,2,3]"

    Index<0 : Skip INDEX indexes from back and then Add/Get/Remove/Set the next index
    Code:
    AHKAAdd("[1,2,3]",BLAH,-1) ;returns "[1,2,BLAH,3]"

~ Usage : Simply type "#Include AHKArray.ahk" at the end of your program, and use the array anywhere in that program.
~ HEX : The data stored can now be HEXed automatically
~ Chaining : All functions are chainable :
Code:
Array := AHKARemove(AHKASet(AHKAAdd(AHKAAdd(AHKAAdd(AHKANewArray(),"A"),"B"),"C"),"B+",2),1)
; [] -> [A] -> [A,B] -> [A,B,C] -> [A,B+,C] -> [B+,C]

Don't see a feature you want? Post it here, and I'll try to add it.

Reserved Strings for Non-HEXing
Inside AHKArray, you may not store strings containing the following symbols IF you are not using HEXing:
Code:
[ ;May be changed
] ;May be changed
| ;May be changed
, ;May NOT be changed

Note: Version 5 adds the possibility to easily change SOME symbols, look at source code of AHKArray.ahk
Note: Contents are automatically HEXed. However this can be disabled in AHKAHex(), see documentation of AHKAHex() below for details

Code Sample
Code:
;Note: This example uses non-HEXed AHKArrays
MyArray := AHKANewArray()
MyArray := AHKAAdd(MyArray,"A")
MyArray := AHKAAdd(MyArray,"B")
MyArray := AHKAAdd(MyArray,"C")
MyLength := AHKASize(MyArray)
MsgBox,MyArray's Length = %MyLength%         ; PRINTS OUT "MyArray's Length = 3"

MyArray := "[A,B,C,[D1,D2,[D3i,D3ii],D4],[E],F]"
MyLength := AHKASize(MyArray)
MyLast := AHKAGet(MyArray)
MyFirst := AHKAGet(MyArray,1)
My3rd := AHKAGet(MyArray,3)
My4th := AHKAGet(MyArray,4)
MyNegative2nd := AHKAGet(MyArray,-1)         ; SKIP 1 FROM BACK TO GET 2ND ELEMENT FROM BACK OF ARRAY
MsgBox,MyArray's Length = %MyLength%         ; PRINTS OUT "MyArray's Length = 6"
MsgBox,MyArray's First Element = %MyFirst%      ; PRINTS OUT "MyArray's First Element = A"
MsgBox,MyArray's Last Element = %MyLast%      ; PRINTS OUT "MyArray's Last Element = F"
MsgBox,MyArray's 3rd Element = %My3rd%         ; PRINTS OUT "MyArray's 3rd Element = C"
MsgBox,MyArray's 4th Element = %My4th%         ; PRINTS OUT "MyArray's 4th Element = [D1,D2,[D3i,D3ii],D4]"
MsgBox,MyArray's 2nd Element From End = %MyNegative2nd%   ; PRINTS OUT "MyArray's 2nd Element From End = [E]"


Version History
Version 0
First release
Basic functions work
Dimensions work up to 2D (in most cases)
Version 1 - 3
Fixed a bug with adding to an empty array.
Added Insert function
Added a "Documentation"
Added a Convert function (now, all the functions that generate Array%i% kind of arrays, can also be used with AHKAArray (the generated variable Array%i% must be global!)
Changed array format
It is now possible to say this:
Code:
Array = "{1,2,3,4}"
To declare an array
Fixed an error with Get()
Version 4
Completely rewrote scripts (thus fixing lots of bugs)
All basic functions work (as tested) for as many dimensions as needed
Array Syntax changed to Flash-Style
Code:
Array = "[1,2,[31,32,[331,332],34],4,5]"

Added Split() function
Version 5
Initial:
Completely rewrote scripts from scratch ... again
All functions work (as tested) for as many dimensions as needed
Sample code included
Array dimensionality is now supported by Parse/Unparse functions (wrongly named so =P)
Patched:
Added Sort() function
Added Swap() function
Added Minimum() function
Added Maximum() function
Added Find() function
Added Reverse() function
Added Merge() function
Added Trim() function
Added Mathematical Floor(),Abs() functions
Upgraded Get() function
Added String() function
Added HEX support
Get() and Set() functions Overloaded
Set() function speed improved
Fixed Remove() function for arrays

Documentation
View Documentation Version 2 Open Alpha (This is the new documentation I'm working on, though, it is nowhere near finished yet.)

Constructor
Code:
Var := AHKANewArray(String="",Delimiter="")
;If String="" Makes a blank new AHKAArray, and returns the array to Var
;If String!="" Acts like AHKASplit (see below)


Size
Code:
Var := AHKASize(Array)
;Gets the length of the array, and returns it to Var


Add
Code:
Var := AHKAAdd(Array,Value,Index=0)
;If Index=0 Adds Value to the end of Array, and returns the array to Var
;If Index>0 Adds Value to Array at Index (all variables that were at index and after, are shifted 1 over), and returns the array to Var
;If Index<0 Adds Value to Array at abs(Index)+1 From End (all variables that were at index+1 from end and after, are shifted 1 over), and returns the array to Var


Get
Code:
Var := AHKAGet(Array,Index1=0,Index2="",Index3="",Index4="",Index5="",Index6="",Index7="",Index8="",Index9="",Index10="")
;If Index=0 Gets the variable at last index in Array, and returns it to Var
;If Index>0 Gets the variable at Index from Array, and returns it to Var
;If Index<0 Gets the variable at abs(Index)+1 from end of Array, and returns it to Var
;Each Index gets the value from the Array present at that point: you can get index [2][3] or [2][3][4] etc...


Remove
Code:
Var := AHKARemove(Array,Index=0)
;If Index=0 Removes value at last index in Array, and returns the array to Var
;If Index>0 Removes value at Index in Array, and returns the array to Var
;If Index<0 Removes value at abs(Index)+1 from end of Array, and returns the array to Var


Set
Code:
Var := AHKASet(Array,Value,Index1=0,Index2="",Index3="",Index4="",Index5="",Index6="",Index7="",Index8="",Index9="",Index10="")
;If Index=0 Sets the variable at last index in array to Value, and returns the array to Var
;If Index>0 Sets the variable at Index in Array to Value, and returns the array to Var
;If Index<0 Sets the variable at abs(Index)+1 from end of Array to Value, and returns the array to Var
;Each Index specifies a place in a dimension: you can set [2][3] or [2][3][4] etc...


Convert
Code:
Var := AHKAConvert(VarName)
;Converts an array of type Variable%Index% to AHKAArray, and returns the generated array to Var (all inside used variables are local, however, Variable%Index% variables must be global)
;Example:
RandomString := "I am a jolly man"
StringSplit, FakeArray, RandomString, %A_Space%
Array := AHKAConvert("FakeArray")

Code:
;Technical Note:
;   This method tries to get the length of the array primarily by %RandomString%, and if that does not work, by %RandomString%0. Meaning, if using a function like StringSplit
;   which returns length in %RandomString%0, make sure RandomString doesn't have a value, otherwise, an empty array will be returned.
;Example of bad usage:
RandomString := "I am a jolly man"
StringSplit, FakeArray, RandomString, %A_Space%
FakeArray := "Nothing Here"
Array := AHKAConvert("FakeArray")

;Array will then contain "[]"


Split
Code:
Var := AHKASplit(String,Delimiter="",CaseSensitive=false)
;Splits the String into different parts, using Delimiter as a separator (String of any length), and returns it as an AHKArray to Var.
;Any characters, including commans and other escaped characters may be used
;Ex:
Array := AHKASplit(String,",")
;If Delimiter is left out, string is splitted into individual characters
;If CaseSensitive=false or is left out, Delimiter is not case sensitive
;If CaseSensitive=true, Delimiter is case sensitive


Sort
Code:
Var := AHKASort(Array,Type=1,Start=0,End=-1)
;If Type=0 Sorts and returns the array to Var, using different values for Start & End, this shouldn't be used
;If Type=1 Sorts the array from smallest to largest. Returns the sorted array to Var.
;If Type=2 Sorts the array from largest to smallest. Returns the sorted array to Var.
;If Type!={0,1,2} Returns the original array to Var.
;If Start=0 or Start=1, array is sorted from start
;If Start>0, array is sorted starting from position Start, indexes before Start are untouched
;If End=-1, array is sorted until the end
;If End>0, array is sorted until index End, indexes after End are untouched
;Current Implementation : QuickSort


Reverse
Code:
Var := AHKAReverse(Array)
;Reverses/Inverses the array. Returns new array to Var.


Minimum
Code:
Var := AHKAMinimum(Array)
;Returns the smallest value found in array to Var.


Maximum
Code:
Var := AHKAMaximum(Array)
;Returns the largest value found in array to Var.


Find
Code:
Var := AHKAFind(Array,Value,Number=1)
;If Number=0 Returns the index of last occurance of value in array to Var
;If Number>0 Returns the index of the Number'th occurance of value in array to Var
;If Number<0 Returns the index of abs(Index)+1'th occurance of value in array starting from end of array to Var
;If value doesn't exist in array, or there aren't as many instances as Number requires, then returns 0 to Var.


Swap
Code:
Var := AHKASwap(Array,IndexA=0,IndexB=0)
;Swaps the values as the two indexes in the array, and returns the new array to Var.
;If Index=0 Swaps value at last index in Array
;If Index>0 Swaps value at Index in Array
;If Index<0 Swaps value at abs(Index)+1 from end of Array


Trim
Code:
Var := AHKATrim(Array,Starting=0,Ending=0)
;Cuts off Starting number of indexes from start of array
;Cuts off Ending number of indexes from end of array
;Returns the resultant array to Var


Merge
Code:
Var := AHKAMerge(ArrayA,ArrayB)
;Adds the content of ArrayB to the end of ArrayA, combining the arrays, and returns the new array to Var.


String
Code:
Var := AHKAString(Array)
;Puts together the contents of the array to turn it into a string with no delimeters, syntax symbols, etc


The following functions were created to be used by other functions

Open
Code:
Var := AHKAOpen(Array)
;"Opens" the array, by removing the Beginning and Ending characters. Returns the new (open) array to Var.
;Example:
Arr := "[52,35]"
Arr2 := AHKAOpen(Arr) ;Arr2 will equal "52,35"


Close
Code:
Var := AHKAClose(Array)
;Opposite of open. "Closes" the array, by adding the Beginning and Ending characters. Returns the new array to Var.
;Example:
Arr := "52,35"
Arr2 := AHKAClose(Arr) ;Arr2 will equal "[52,35]"


Parse
Code:
Var := AHKAParse(Array)
;Parses the array removing all inner arrays, and replacing them with the | symbol. Returns the new array to Var.


ParseFirst
Code:
Var := AHKAParseFirst(Array)
;Parses the array removing only the FIRST inner array, and replacing it with the | symbol. Returns the new array to Var.


GetDimension
Code:
Var := AHKAGetDimension(Array,Index)
;Gets the Index'th inner array. (Skips Index-1 arrays, and returns the next one). Returns the found value to Var.


GetFirstDimension
Code:
Var := AHKAGetFirstDimension(Array)
;Gets the 1st inner array found. Returns the found value to Var.


Unparse
Code:
Var := AHKAUnparse(ParsedArray,UnparsedArray)
;Inside parsed array ParsedArray, replaces all the |'s with inner arrays from UnparsedArray. Returns the new array to Var.


Floor
Code:
Var := AHKAFloor(number)
;Equivalent of the mathematical "greatest integer".
;Find the greatest integer that is less than or equal to number, and returns it to Var.
;Note: Bad method, slow.


Abs
Code:
Var := AHKAAbs(number)
;Equivalent of the mathematical "absolute value".
;Returns the absolute value of the number to Var.


CharToHex
Code:
Var := AHKACharToHex(char)
;Returns the HEX equivalent of the char's ASCII value to Var. (Maxium of ONE character in char)


CharFromHex
Code:
Var := AHKACharFromHex(HEX)
;Returns the character of the ASCII value equivalent to HEX to Var. (Must be 2 characters in char (HEX))


Hex
Code:
Var := AHKAHex(String,Way,Enabled=true)
;If Way=1 Converts String into HEX and returns it
;If Way=0 Converts String from HEX and returns it
;If Way!={1,0} Converts String into HEX and returns it
;Change "Enabled=true" to "Enabled=false" in this method to disable HEXing for AHKArray


GetSimple
Code:
Var := AHKAGetSimple(Array,Index=0)
;Used by AHKAGet()
;Please refer to AHKAGet() for documentation
;This can only get ONE index


SetSimple
Code:
Var := AHKASetSimple(Array,Value,Index=0)
;Used by AHKASet()
;Please refer to AHKASet() for documentation
;This can only set in ONE dimension


Last edited by olegbl on Sun May 04, 2008 6:40 pm; edited 43 times in total
Back to top
View user's profile Send private message
jonny



Joined: 13 Nov 2004
Posts: 3005
Location: Minnesota

PostPosted: Wed Dec 13, 2006 8:17 am    Post subject: Reply with quote

Since there are still several commands and functions that will only output to a regular array, how about a function that converts a regular array to an AHKArray? It would be a lot easier to use then, instead of having to do something like this:

Code:
NewArray := AHKANewArray()
Loop %Array0%
   AHKAAdd(NewArray,Array%A_Index%)


Edit: Did you add that while I wasn't looking? Laughing

Any plans for hash tables, stacks, queues, trees? Smile
Back to top
View user's profile Send private message
olegbl



Joined: 13 Dec 2006
Posts: 19

PostPosted: Wed Dec 13, 2006 9:40 pm    Post subject: Reply with quote

Yup, added it behind your back Razz
I'm currently upgrading the whole script, and slightly changing the format, to make it a lot easier to use, etc...
Will post V3 soon.

Edit: Done

Edit2: Oh, didn't see the question, looked like signature XD
Well, for now, this is pretty much as far as I'm going, though, later on I will probably add everything from full character storing compatibility, to auto-sorting, and byte-storage
(the whole reason I did this, is because I'm making a code-wise self updater (i already made like 3, but they suck), and I need local variable arrays for it, (to make it much more efficient, and not global)

Edit3: Er, I've decided against taking this to an actual array level, that can be done in AHKv2 or whatever it is thats being talked about, but for now, I think this is plenty good enough to have...
If anyone wants to do it though, haha, go ahead =P
Back to top
View user's profile Send private message
Mistrel



Joined: 12 Sep 2005
Posts: 187

PostPosted: Wed Jan 10, 2007 5:55 am    Post subject: Reply with quote

I have some unfinished functions I wrote for another AHK project that might be useful to you. It has "remove from queue" and "remove from stack" support for standard AHK (fake) arrays. There are also some other array functions like PrintArray and CollapseArray.

http://www.autohotkey.com/forum/viewtopic.php?t=13885&highlight=another+method
Back to top
View user's profile Send private message
Guest






PostPosted: Sun Mar 04, 2007 3:42 am    Post subject: Reply with quote

is any1 reading this if so i have this problem this code produces a msgbox with zero on it what am i doing wrong
Code:
charArray := AHKANewArray()
Loop,5
   AHKAAdd(charArray,65)
var := AHKASize(charArray)
MsgBox %var%

Thanks
Back to top
pantagruel



Joined: 08 Oct 2006
Posts: 52
Location: denmark

PostPosted: Sun Mar 04, 2007 7:06 pm    Post subject: invalid array format Reply with quote

hi,

It seems no matter what I do I get back an invalid array format error, here are the various ways I have tried to declare an array:

xarray = [1, 2, 3]

xarray = "[1,2,3]"

xarray := "[1,2,3]"

xarray := "{1,2,3}"

xarray = "{1,2,3}"

xarray := AHKANewArray()

xarray := AHKAAdd(xarray,1)

after which I try to do the following



xindex = 1



xindex := 1

either one I don't care

finally


#r::
xvalue := AHKAGet(xarray,1)

return

which tells me that I have an invalid array format.

thank you
Back to top
View user's profile Send private message
Guest






PostPosted: Mon Mar 05, 2007 12:31 am    Post subject: Reply with quote

Yeah im having the same problem i posted my code before and it seems im doing it right but the array never initializes and i always get array format errors
Back to top
pantagruel



Joined: 08 Oct 2006
Posts: 52
Location: denmark

PostPosted: Tue Mar 06, 2007 10:28 am    Post subject: version problems I suppose Reply with quote

I'm going to suppose that the culprit is the version of Autohotkey used. I'm using 1.0.46.09, anyway I figure that, given the multitudinous variations I've done that it is not going to work.
Back to top
View user's profile Send private message
olegbl



Joined: 13 Dec 2006
Posts: 19

PostPosted: Wed Mar 14, 2007 5:24 am    Post subject: Reply with quote

Err, sorry for a rather long non-reply.
Was busy, didn't do AHK.
Right now, I'm writing a much better, more efficient version of AHKArray (from scratch).
As for the questions, I'll try to answer (though late):
Re-Guest:
when you use functions, you gotta set your array to equal them...
Code:
charArray := AHKANewArray()
Loop,5
   AHKAAdd(charArray,65)
var := AHKASize(charArray)
MsgBox %var%
doesn't work, but
Code:
charArray := AHKANewArray()
Loop,5
  charArray := AHKAAdd(charArray,65)
var := AHKASize(charArray)
MsgBox %var%
should work.

Re-Pantagruel:
Code:
xarray := "[1,2,3]"

should work.
Code:

xvalue := AHKAGet(xarray,1)
MsgBox, %xvalue%

should work too.

However, if they don't, maybe it is because of AHK version like was mentioned...
Well, I'm working on AHKArray v5 (im using the newest AHK version to do so)... So, I'll get it done in a few days.
(Hopefully including some code examples =P)

PS Don't forget the #include AHKArray.ahk at the end of your prog.

==EDIT==
Worked until midnight, but here's V5... hopefully should work for pretty much everything... didn't do TOO much testing yet..
Back to top
View user's profile Send private message
guest1
Guest





PostPosted: Sat Mar 31, 2007 4:47 pm    Post subject: Reply with quote

great! and perfect!!

also really impressive documentation!!!
Back to top
olegbl



Joined: 13 Dec 2006
Posts: 19

PostPosted: Sat Mar 31, 2007 11:13 pm    Post subject: Reply with quote

Thanks!

Next version will be out sometime soon.
(I think there's 3 new methods, maybe more)
(Not this weekend though, too busy...)
_________________
Real arrays are here.
Customizable self-updaters are too.
Back to top
View user's profile Send private message
Elevator_Hazard



Joined: 28 Oct 2006
Posts: 303
Location: Illinois

PostPosted: Sun Apr 01, 2007 2:25 am    Post subject: Reply with quote

So you can't use certain characters? how about converting to hex so that all letters can be used, and you can use many more delimiters. Of course the size of it will be multiplied by 2...
_________________
Changed siggy at request of ahklerner Very Happy
Back to top
View user's profile Send private message
olegbl



Joined: 13 Dec 2006
Posts: 19

PostPosted: Sun Apr 01, 2007 4:05 am    Post subject: Reply with quote

Well, I was thinking of instead putting 4 optional parameters at the end of each function specifying delimeters, so they could be customized.
However, I do like the HEX idea, thanks!
Might implement that in the next version.
_________________
Real arrays are here.
Customizable self-updaters are too.
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Sun Apr 01, 2007 12:43 pm    Post subject: Reply with quote

Maybe have a initialize function to change the characters. Then it only needs to be set once.

How do I get values from a mutiple dim array? I have to call AHKAGet multiple times, right? Could there be a function that gets the value by, e.g.
AHKAValue(MyArray, IndexDim1, IndexDim2, IndexDim3, etc. up to IndexDim10)?
By making the indicies optional and limited to 10 dims it should be possible.

Is there an option to sort numerically?

Just my two cents.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
olegbl



Joined: 13 Dec 2006
Posts: 19

PostPosted: Sun Apr 01, 2007 5:00 pm    Post subject: Reply with quote

Well, to make an init function, the variables would have to be global, which is what I've been trying to avoid...
I did however already implement HEX... This removes the ability to say
Code:
Array :="[a,b,c]"
;Instead, you must now say
Array := "[61,62,63]"

And it also limits the storage capacity to the first 255 ascii chars, but it does remove the limitation for dilimeter characters.
I added an easy way to enable/disable HEXing, which requires a simple change of "false" to "true" in two places.

To get a value from multiple dimension you could say:
Val := AHKAGet(AHKAGet(AHKAGet(Array,X),Y),Z)
But, it would be rather easy to implement the multiple index thing, so I'll try to do it.

Numerical sorting, I think is the same in AHK, so the sort method should do that. (it basically compares values to each other using the > and < symbols which work both ways as far as I know)

Thanks for you suggestions!

I'll try to get all this stuff implemented and released in the next version...
_________________
Real arrays are here.
Customizable self-updaters are too.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group