Arrays in AfterGRASP
There are two types of "arrays" in GLPRO. The first type is basically a way to access a buffer created with MEMNEW. I don't recommend using this
except for special cases, and I haven't even decided how to handle it in AfterGRASP.
The other type is much simpler to use, and requires no declaration (no MEMNEW),
so you just start using, and the index into an array can be anything, not just a number.
So what does:
set a[5][30] 200
really do?
In GLPRO it actually creates a variable called a_5_30, so the above is the same as:
set a_5_30 200
So for:
set a[@i]
you could write:
set a_$@i
The problem is, what if you wanted this?
set a[@i[@j]+2]
How would you write that?
set a_$(@(i_$@j)+2)
Pretty hard to follow. Even I had to read the above line several times before I thought it might be right.
Also NEVER mix usage of arrays with build variable names like above because in AfterGRASP arrays do not work like this.
There is no a_5_30 variable.
Instead "a" is a variable of type "array", and it contains a value indexed
by "5", and that value is another array (two dimensional) which contains a value indexed by "30".
So although:
set a[5][30] 200
text @a_5_30
will work in GLPRO, it won't in AfterGRASP. You must use:
set a[5][30] 200
text @a[5][30]
Of course the style of code you have been using instead of arrays, where you build each variable name with string
concatenation, will work in AfterGRASP, but it will be much slower than using real arrays.
So besides being more readable, and easier to program, using the arrays syntax will give you a even bigger performance boost in AfterGRASP.
Also avoid using the same name of an array and normal variable like this:
set a[30] hello
set a goodbye
textln @a[30] ; WILL FAIL!
There are some powerful features in AG to get the contents of an array or its index.
For instance, to get a sorted list of all the unique lines in a file:
for l in fileget(example.txt)
set lines[@l] 1
next
filesendln uniqlines.txt arrayindex(lines)
Or
for l in fileget(example.txt)
set lines[@l] @l
next
filesendln uniqlines.txt @lines
Or
for l in fileget(example.txt)
set lines[@l] @l
next
for l in strlist(@lines)
filesendln uniqlines.txt @l
next
"Real arrays sounds like a good step forward. But is there any chance of a replacement for the square bracket in variable names? My top choice would be the good old dot (as in @document.text.@n.x1 )."
Not a chance, at least for periods. Some examples of why periods would never work:
set a[1.53][z] answer
set a.1.53.z answer
set a[hello.txt] 0
set a.hello.txt 0
You see? Not only are periods valid in floating point numbers, they are also a valid part of a string.