There have been requests for real arrays in ahk for a long time, and people have come up with several not to shabby solutions by putting the array in one var. (e.g. AHKA, SimpleArray) But for several (good) reasons, some don't want to use those, and instead choose to use AHK's psuedo arrays.

Psuedo arrays are OK, but they're limited. There are some things that psuedo-arrays just can't do, like passing an array as a func param. Other things are just a big pain and give people headaches, like inserting or removing an element in an array. (not overwriting and clearing) What I present here is a way to alleviate these headaches.

If you are already familiar with the terms, These functions can now shift, unshift, push, pop, insert, swap, and rotate psuedo-array elements (+more). Otherwise, you could say they can add elements anywhere in an array, (specific # from beginning or end, or at the beginning or end) and also delete (not clear) elements from an array, so the array actually becomes shorter.

Unfortunately, they are limited to only affecting global arrays since they naturally won't have access to a function's internal variables. It also requires the number of elements in the array contained be in the 0 element of the array. (e.g. "MyArray0") It is also limited to only working on flat (single dimensional) arrays.

There are two main funcitons:
  • pgArray_Insert("MyArray", Idx, ele1 [, ele2, ele3, ...])
    Puts any number of elements into an array starting at any point. Returns the length of the new array.
    Idx=1; prepend the given elements to the beginning of the array (aka "unshift")
    Idx=0; append the given elements to the end of the array (aka "push")
    Idx>0 / Idx<0; insert the given elements before this index: 1 based from the beginning / -1 based from the end
  • pgArray_Shift("MyArray" [, Idx=1, HowFar=1])
    Shifts following elements ontop of the elements at Idx + HowFar and clears the rest at the end. Returns the contents of the first removed element.
    Don't specifiy an Idx, or use 1 to delete the first element. (aka "shift")
    Idx=-1; delete the last element of the array. (aka "pop")
    Idx>0 / Idx<0; 1 based from the beginning / -1 based from the end
    specify HowFar for how many elements to delete. (e.g. Idx=1, HowFar=2 would delete the first two elements)