PDA

View Full Version : Having fun with Arrays!



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!

Sjoe
03-30-2013, 01:38 AM
Looks useful :) gonna read this thru.

rj
03-30-2013, 01:45 AM
I needed this after that skype convo we had

sahibjs
03-30-2013, 01:48 AM
Gonna need this after an IRC convo I had...

Sin
03-30-2013, 02:01 AM
Awesome, hope its useful :)

sahibjs
03-30-2013, 02:50 AM
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.




Alright Sin, LOADS of questions coming your way:
1) Is there a particular reason you didn't count up to MyArray[4] in the long way?
2) You've stored simple integers in MyArray, what if I have to save coordinates? Is it the same?

I've been trying to get my head around TPA's all day long :P

Sin
03-30-2013, 02:59 AM
Alright Sin, LOADS of questions coming your way:
1) Is there a particular reason you didn't count up to MyArray[4] in the long way?
2) You've stored simple integers in MyArray, what if I have to save coordinates? Is it the same?

I've been trying to get my head around TPA's all day long :P

1) Not really, just got bored I guess
2) [Point(x,y),Point(x,y)]

sahibjs
03-30-2013, 02:49 PM
1) Not really, just got bored I guess
2) [Point(x,y),Point(x,y)]

So for some RS applications, if I wre to store multiple tree colors in an an array, from the base to the trunk to the top, and I were to call on that array from a FindObj procedure, would using the array allow me accuracy in finding the tree? Or is that not how you utilize arrays?

rj
04-03-2013, 09:46 PM
show something about matching array strings
program ArrayTest;
Procedure ArrayT;
Var
FoodArray:TStringArray;
s:string;
i, j:integer;
begin
s := 'beans';
FoodArray := ['chicken','rice','beans','lobster']
for i:= 0 to high(FoodArray) do
begin
if (j>high(FoodArray)) then exit;
writeln('current array:' + FoodArray[j])
if s = FoodArray[j] then
begin
writeln('matched: ' + FoodArray[j])
exit;
end;
j := j+1
end;
end;
begin
ArrayT;
end.

xtrapsp
04-04-2013, 10:35 AM
sahibjs

You said you were still struggling. So I'm going to try and make this easier for you.

Imagine you have a broom. And you're holding it out like so:

__________________________________________________ _

Now imagine we have 5 buckets.

\_/ \_/ \_/ \_/ \_/

So, Each of those buckets can hold water.

So lets Hook the Buckets onto the broom!

_________________
\_/ \_/ \_/ \_/ \_/

So the buckets of water (Information, data, values, variables, w.e) are inside the Broom as a collective


BroomArray[0] := 0; // First bucket
BroomArray[1] := 10; //Second Bucket
BroomArray[2] := 20; // Third Bucket
BroomArray[3] := 30; // Fourth Bucket
BroomArray[4] := 40; // Fifth Bucket

Notice how it starts at 0 and not 1!

Now imagine it's measuring the amount of Gallons in each bucket :) so


BroomArray[0] := 0; // 0 Gallons
BroomArray[1] := 10; // 10 Gallons
BroomArray[2] := 20; // 20 Gallons
BroomArray[3] := 30; // 30 Gallons
BroomArray[4] := 40; // 40 Gallons

//Grouping; This is much easier, and cleaner.
BroomArray:= [0,1,2,3,4];

So the [#] is the number of the bucket on the broom and the array is called broom :)

randy marsh
04-24-2013, 05:21 PM
Think would be good if you could show how you could use this for a runescape script .

Or the different uses of arrays for scripting ;)

Pakyakkistan
03-12-2014, 05:29 PM
Thanks for the guide man.
Helping me further my understanding of Arrays.

guerr
03-20-2016, 07:23 PM
Used it today, when i needed to check worlds, thanks!