Sin
03-30-2013, 01:30 AM
Hello and welcome to my first tutorial in a long time. In this one, we'll be messing around with arrays.
If you don't understand something, feel free to post!
Table of Contents
What is an array
What are arrays useful for
Declaring an array
Initializing an array
Giving an array index a value
For..to..do loops and arrays
ATPAs and TPAs + for loops
Nested Multidimensional Arrays
What are arrays?
An array is nothing more than a 'list' of different values. These values can range from integers, to Points, to Strings. The lists are stored in a easy to access format and you can have quite a bit of fun with them, at the same time as making your code more efficient.
What are arrays useful for?
Arrays are useful for numerous things. You can store numerous values in them to efficientize your code, you can use 2DArrays to create intricate functions, and best of all, it makes it seem as if you know what you're doing!
Declaring an array
There are different ways to declare an array. You can either create a new type to define your own array, you can use the Pascal keywords, or you can use the types built into Simba.
//Creating an array via Pascal keywords
//This is extremely simple, once you declare your variable name, you simply add 'array of X', where X is your value type.
//Examples:
var
MyArray0:array of Integer; //Declares an array of an integer.
MyArray1:array of String; //Declares an array of a String.
MyArray2:array of Extended; //Declares an array of an extended integer (This is basically the Pascal version of a Java Double)
MyArary3:array of Boolean; //Declares an array of true or false statements. You will use these very rarely IMO, i've never had the need to use this.
//Creating an array via Simba keywords
//This is also very simple. The format is usually: 'T' + value type + 'Array'.
//Examples:
var
MyArray0:TIntegerArray; //Declares an array of an integer.
MyArray1:TStringArray; //Declares an array of an String.
MyArray2:TExtendedArray; //Declares an array of an extended integer.
MyArray3:TVariantArray; //Declares a variant array.
//Creating your own array name
//This is fairly simple, look at AbuJwka's type tutorial for more info
//Example:
type
TSinArray = array of Variant;
//You can then declare a variable normally.
var
Sin:TSinArray;
Initializing an array.
Without initalizing an array, your script will throw an out of range error, like this:
http://puu.sh/2qoUS
Although the script will compile, the IDE will receive a Runtime Error.
You can circumvent these by doing the following:
SetLength(a,aL);
Description of the parameters:
a: The name of the array
aL: The length you want the array to be.
Giving an Array index a value
After you've set the length of an array, you can then go on and give it's indexes a value.
For the purpose of this tutorial, we're just going to mess with Integer Arrays, as they're the easiest.
A key point is that array indexes start at 0, not 1!
var
MyArray:TIntegerArray;
begin
SetLength(MyArray,5);
//You can assign array values two seperate ways. Individually or Grouping
//Individually; This is painstaking, and not recommended unless unavoidable.
MyArray[0] := 0;
MyArray[1] := 1;
MyArray[2] := 2;
//Grouping; This is much easier, and cleaner.
MyArray := [0,1,2,3,4];
end.
Both ways work, it's just preference.
For..to..do loops and arrays
For loops are my favourite things to use, hands down. Them combined with arrays are just plain amazing.
With for loops, you can loop through array values without going through each individual one manually.
Here's an example, taken from my Trivia Game script:
procedure AddNamePoint(Name:String);
var
i:Integer;
begin
//Set array length to match Names array
SetLength(Points,Length(Names));
//initializing vars, setting base values to 0
Names[0] := 'Default';
Points[0] := 0;
for i := 1 to High(Names) do
begin
if (Names[i] = Name) then
begin
Points[i] := Points[i] + 1;
writeLn('One point added to ' + ToStr(Names[i]));
Exit;
end;
end;
writeLn('Name didnt exist in array, adding now.');
//incase index[i] doesnt exist
i := Length(Names);
SetLength(Names,i+1);
Names[Length(Names)-1] := Name;
AddNamePoint(Name);
end;
As you can see, the for i := 1 to High(Names) do goes through all the values in the Names array, from the index of 1, to the highest index of Names.
Here's a slightly less complicated example:
program new;
{$i srl/srl.simba}
var
MyArray:TIntegerArray;
i:Integer;
begin
SetupSRL;
SetLength(MyArray,5);
MyArray := [0,1,2,3,4];
for i := Low(MyArray) to High(MyArray) do
writeLn(ToStr(MyArray[i]));
end.
What that does is it loops through ALL index positions of MyArray and writes their values.
The output:
SRL Compiled in 0 msec
0
1
2
3
4
Successfully executed.
ATPAs and TPAs + for loops
This will be fairly brief, but informative nonetheless.
A TPA is an array of a TPointArray. An ATPA is a multidimensional array, meaning it's an array of an array of TPoints.
You can use sorted ATPAs in parameters which require a for loop like this:
if FindColorsSpiralTolerance(P.x,P.y,TPA,Color,3,3,51 8,310-50,Tol) then
begin
SortTPAFrom(TPA,Center);
ATPA := TPAtoATPAEx(TPA,5,5);
for i := Low(ATPA) to High(ATPA) do
begin
TPABox := GetTPABounds(ATPA[i]);
What that snippet does:
Stores the TPoints of where it finds the colors into the variable 'TPA'
Sorts the TPA from closest to the midpoint of the screen, to the outsides
Creates an ATPA from the sorted TPAs into 5x5 boxes
Uses a for loop to get the boundaries of an ATPA
You might notice that the function name is GetTPABounds, where I passed an ATPA.
I passed the ATPA in a for loop, which means that in essence I actually passed a TPA.
Nested Multidimensional Arrays
var
ATPA:T2DPointArray;
TPA:TPointArray;
i,j:Integer;
begin
//Get an ATPA
ATPA := SortTPAFromMidPoint(TPA,5);
//create a nested for loop
for i := Low(ATPA) to High(ATPA) do //Position 0 to High in the ATPA
for j := Low(ATPA[i]) to High(ATPA[i]) do //Position 0 to High in the TPA
Mouse(ATPA[i][j].x,ATPA[i][j].y,5,5,mouse_Left); //clicks the POINT located in the TPA at position j.
end;
As you can see in the code, the first for loop loops through the ATPA, to find the index of the TPA.
The second for loop loops through the TPA, to find the value of a TPoint in the TPA.
Conclusion
Arrays are very versatile, and are extremely useful. We like to see them in member's apps, especially TPAs and ATPAs! This guide took me a while to write,
so feel free to hit that rep button!
If you don't understand something, feel free to post!
Table of Contents
What is an array
What are arrays useful for
Declaring an array
Initializing an array
Giving an array index a value
For..to..do loops and arrays
ATPAs and TPAs + for loops
Nested Multidimensional Arrays
What are arrays?
An array is nothing more than a 'list' of different values. These values can range from integers, to Points, to Strings. The lists are stored in a easy to access format and you can have quite a bit of fun with them, at the same time as making your code more efficient.
What are arrays useful for?
Arrays are useful for numerous things. You can store numerous values in them to efficientize your code, you can use 2DArrays to create intricate functions, and best of all, it makes it seem as if you know what you're doing!
Declaring an array
There are different ways to declare an array. You can either create a new type to define your own array, you can use the Pascal keywords, or you can use the types built into Simba.
//Creating an array via Pascal keywords
//This is extremely simple, once you declare your variable name, you simply add 'array of X', where X is your value type.
//Examples:
var
MyArray0:array of Integer; //Declares an array of an integer.
MyArray1:array of String; //Declares an array of a String.
MyArray2:array of Extended; //Declares an array of an extended integer (This is basically the Pascal version of a Java Double)
MyArary3:array of Boolean; //Declares an array of true or false statements. You will use these very rarely IMO, i've never had the need to use this.
//Creating an array via Simba keywords
//This is also very simple. The format is usually: 'T' + value type + 'Array'.
//Examples:
var
MyArray0:TIntegerArray; //Declares an array of an integer.
MyArray1:TStringArray; //Declares an array of an String.
MyArray2:TExtendedArray; //Declares an array of an extended integer.
MyArray3:TVariantArray; //Declares a variant array.
//Creating your own array name
//This is fairly simple, look at AbuJwka's type tutorial for more info
//Example:
type
TSinArray = array of Variant;
//You can then declare a variable normally.
var
Sin:TSinArray;
Initializing an array.
Without initalizing an array, your script will throw an out of range error, like this:
http://puu.sh/2qoUS
Although the script will compile, the IDE will receive a Runtime Error.
You can circumvent these by doing the following:
SetLength(a,aL);
Description of the parameters:
a: The name of the array
aL: The length you want the array to be.
Giving an Array index a value
After you've set the length of an array, you can then go on and give it's indexes a value.
For the purpose of this tutorial, we're just going to mess with Integer Arrays, as they're the easiest.
A key point is that array indexes start at 0, not 1!
var
MyArray:TIntegerArray;
begin
SetLength(MyArray,5);
//You can assign array values two seperate ways. Individually or Grouping
//Individually; This is painstaking, and not recommended unless unavoidable.
MyArray[0] := 0;
MyArray[1] := 1;
MyArray[2] := 2;
//Grouping; This is much easier, and cleaner.
MyArray := [0,1,2,3,4];
end.
Both ways work, it's just preference.
For..to..do loops and arrays
For loops are my favourite things to use, hands down. Them combined with arrays are just plain amazing.
With for loops, you can loop through array values without going through each individual one manually.
Here's an example, taken from my Trivia Game script:
procedure AddNamePoint(Name:String);
var
i:Integer;
begin
//Set array length to match Names array
SetLength(Points,Length(Names));
//initializing vars, setting base values to 0
Names[0] := 'Default';
Points[0] := 0;
for i := 1 to High(Names) do
begin
if (Names[i] = Name) then
begin
Points[i] := Points[i] + 1;
writeLn('One point added to ' + ToStr(Names[i]));
Exit;
end;
end;
writeLn('Name didnt exist in array, adding now.');
//incase index[i] doesnt exist
i := Length(Names);
SetLength(Names,i+1);
Names[Length(Names)-1] := Name;
AddNamePoint(Name);
end;
As you can see, the for i := 1 to High(Names) do goes through all the values in the Names array, from the index of 1, to the highest index of Names.
Here's a slightly less complicated example:
program new;
{$i srl/srl.simba}
var
MyArray:TIntegerArray;
i:Integer;
begin
SetupSRL;
SetLength(MyArray,5);
MyArray := [0,1,2,3,4];
for i := Low(MyArray) to High(MyArray) do
writeLn(ToStr(MyArray[i]));
end.
What that does is it loops through ALL index positions of MyArray and writes their values.
The output:
SRL Compiled in 0 msec
0
1
2
3
4
Successfully executed.
ATPAs and TPAs + for loops
This will be fairly brief, but informative nonetheless.
A TPA is an array of a TPointArray. An ATPA is a multidimensional array, meaning it's an array of an array of TPoints.
You can use sorted ATPAs in parameters which require a for loop like this:
if FindColorsSpiralTolerance(P.x,P.y,TPA,Color,3,3,51 8,310-50,Tol) then
begin
SortTPAFrom(TPA,Center);
ATPA := TPAtoATPAEx(TPA,5,5);
for i := Low(ATPA) to High(ATPA) do
begin
TPABox := GetTPABounds(ATPA[i]);
What that snippet does:
Stores the TPoints of where it finds the colors into the variable 'TPA'
Sorts the TPA from closest to the midpoint of the screen, to the outsides
Creates an ATPA from the sorted TPAs into 5x5 boxes
Uses a for loop to get the boundaries of an ATPA
You might notice that the function name is GetTPABounds, where I passed an ATPA.
I passed the ATPA in a for loop, which means that in essence I actually passed a TPA.
Nested Multidimensional Arrays
var
ATPA:T2DPointArray;
TPA:TPointArray;
i,j:Integer;
begin
//Get an ATPA
ATPA := SortTPAFromMidPoint(TPA,5);
//create a nested for loop
for i := Low(ATPA) to High(ATPA) do //Position 0 to High in the ATPA
for j := Low(ATPA[i]) to High(ATPA[i]) do //Position 0 to High in the TPA
Mouse(ATPA[i][j].x,ATPA[i][j].y,5,5,mouse_Left); //clicks the POINT located in the TPA at position j.
end;
As you can see in the code, the first for loop loops through the ATPA, to find the index of the TPA.
The second for loop loops through the TPA, to find the value of a TPoint in the TPA.
Conclusion
Arrays are very versatile, and are extremely useful. We like to see them in member's apps, especially TPAs and ATPAs! This guide took me a while to write,
so feel free to hit that rep button!