Jump to content
Macro Express Forums

My New Array Technique Is Unstoppable!


Recommended Posts

Apologies for the hyperbolic title. I have been visiting this site:

my new filing technique is unstoppable


so I couldn't resist ;)


Anyway, I'm pleased to announce my new (AFAIK) array technique for Macex which

I believe can be used for many purposes.

For instance, you could use it to address variables in a loop, or just to create (virtually) unlimited variables, far more than Macex's 99 variable limit. It also opens up Macex to more complex data-manipulation such as sorting. As an example, check out the Bubble Sort routine I implemented in the Macro Express 3.x Third Party Tools forum http://pgmacros.com/community/index.php?showforum=9.


My method involves creating an array within a text variable. While this has been proposed before, I believe my method is more efficient than previous implementations.


My technique can retrieve any element in an array of any size by using only 6 operations, whereas the previous solutions required a number of operations proportional to the size of the array. That is, the bigger the array, the more operations needed to retrieve data.




My technique involves appending unique array element (record) markers to the start and the end of each element, in this format:

Axxx~ Element ~xxxA


where xxx is an integer, Axxx~ is the begin array element marker in the Text variable,

and ~xxxA is the end array element marker.


So for example, if we have the following records:



The array would look like this:



If the array is created from user input, you can just insert the markers on-the-fly for each entry. If you want to create the array using a loop, you can use a variable reference like: A%N2%~%T1%~%N2%A


where N2 would be the loop counter, and T1 would be the record which will become an array element.




To read a particular element from the array, say, no. 13:

1. Look for position of A13~ (the begin array element marker) in the Text variable.

2. Look for position of ~13A (the end array element marker) in the Text variable.

3. Calculate length of element by subtracting the position of the begin marker from the end marker.

4. Determine length of the begin array marker (i.e. length of A13~)

5. Calculate offset for the element by adding length of begin array marker to its position.

6. Copy the the specified number of characters from the array text variable, starting at the offset position.


That might not sound very efficient, but note that the number of operations to retrieve any element always remains the same, whether it is the 10th element or the 500th element.




Say, for instance, you want to write the word "foo" to the 13th element in the array. If the array is empty, this can be accomplished in two operations:


1. Append ~13A to the word, resulting in foo~13A

2. Replace ~13A (the end array element marker) in the Text variable with foo~13A


If the array is not empty, you will need to read the existing element first, which involves 6 operations described above, followed by another two operations:


Assuming the 13th element contains "fighter",

1. Append ~13A to the word currently in the array, resulting in fighter~13A

2. Replace fighter~13A in the Text variable with foo~13A


I hope this array technique will be useful to others. For an example of its use, refer to the Bubble Sort macro.




Link to comment
Share on other sites

  • 1 year later...


I nevefr cease to learn new stuff in this forum !!


I used to program in MUMPS language (now "m" language)


They used an "^" as delimiter, and used counting (the code could select 2nd, third, etc position, data inside any delimiter; "^" was used since most data did not ever use it for other data stuff


Anyway, thanks


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...