XxKanexX
04-08-2006, 01:29 PM
About Arrays:
Arrays are excellent for taking place of those countless integers or making a variable that possibly can always be upgraded and another value set to it.
Arrays may seem hard, but once you know how to use them you will come to realise that they are extremely easy aswell as handy.
Explaining an Array:
An Array is somewhat like a never ending variable. It can be one root variable with however many user set values. Seem confusing? - Think of it as a Tree. All of the Array extensions are the branches. So if i had an Array named Numbers and it had a user set number of branches of 20, then there is 20 branches which can all have there own value.
The Array layout:
Arrays are first changed or called by their root name. Next is the number of the array extension. In this example, The arrays name is Numbers and the extension number is 2.
Numbers[2]:= Data;
If the Arrays name was Kane[/b] and the extension number was [i]5 then it would be:
Kane[5]:= Data;
The number in between the brackets is, like explained above, similar to a tree's branches.
Kane:= Data;
Kane:= Data;
Kane:= Data;
Each branch can have it's own data all installed into 1 root variable. :) It all saves alot of valuable time and lines. It can also keep the code looking clean and a little more proffesional.
Declaring an Array:
An array is declared like any other variable. Although it also has another part where you can set where the array starts, and ends.
In this example, the Arrays name is Numbers, It starts at 1 and Ends at 3.
[b]var
Numbers: Array[1..3] of Integer;
From the example above, I'll explain from left to right what everything means.
Numbers is the arrays name, it's followed by a Colon to explain to Scar that the name has ended and to continue with the variables values.
The array name along with a colon is followed by "Array", which tells Scar that this variable is an Array.
Most people can get confused by the next bit. It's a Start number followed by 2 dots then an End number in brackets. Since i have put "1..3" it means that the Array "Numbers" has 3 values:
Numbers[1]:= Data;
Numbers[2]:= Data;
Numbers[3]:= Data;
Just like "Numbers" is the Trees root and the numbers in between the brackets are the branches. The start and end number can be anything, As long as the Start is smaller than the end number. Each branch can have it's own value:
Numbers[1]:= 1;
Numbers[2]:= 50;
Numbers[3]:= 3;
"of" is simply the root variable of the Array. Which can be followed by whatever you want the array and it's extensions to be:
of Integer, String, Byte, Char, ect.
I have chosen Integer. Which means ever branch/extension of the arrays value is an Integer.
Values and Root Array Variables:
A root Array Variable is the Arrays branches/extensions variable value. Like a normal variable, You can set it to be a: [i]String, Integer, Byte, Char, Boolean, ect. This is the same with an Array. You can use any of the normal root Variable choices.
So if i had:
of Boolean;
It would mean that that Arrays extensions/branches are all booleans. Which means they can all be either True or False. If it was:
of String;
Then all the values could be Strings, which leads me to the next bit of the tutorial. Explaining how to set an Arrays value.
We'll use this Array as an example:
var
Numbers: array[1..3] of Integer;
Which, if you have read above, Means that my root array is Numbers, It's start is [i]1 and end is 3 And all the values are Integers.
Giving each Extension/branch a value:
Working with the example in the previous part of the tutorial, we need to set the Array Numbers's Branches/Extensions values. To do this we would do:
var
Numbers: array[1..3] of Integer;
begin
Numbers[1]:= 5;
Numbers[2]:= 6;
Numbers[3]:= 2;
end.
Above means that in the main loop i have set each Branch/Extensions value to whatever i want it to be, aslong as it's an integer as that's what the root variable of the Array is.
The data can be anything you want, Just like a normal variable.
If i were to make the root Array Variable a string i could do:
var
Names: array[1..4] of String;
begin
Names[1]:= 'Kane';
Names[2]:= 'Ben';
Names[3]:= 'Krazy';
Names[4]:= 'Pyro';
end.
The other ways of Arrays:
Here i'll just show you some scripts of all the possibilites of declaring an array and setting it's values.
The Start and End points can be anything you want:
var
Kane: array[3..6] of Integer;
Just aslong as the Start number was smaller than the end number.
Above i start at 3 and end at 6. This means my branches/extensions can be 3 - 6:
var
Kane: array[3..6] of Integer;
begin
Kane[3]:= 5;
Kane[4]:= 2;
Kane[5]:= 65;
Kane[6]:= 0;
end.
The name can be anything you want:
var
BlaLaFLO: array[0..6] of String;
The root array variable can be any variable:
var
Kane: array[1..2] of Integer;
var
Kane: array[1..2] of Boolean;
var
Kane: array[1..2] of String;
var
Kane: array[1..2] of Char;
var
Kane: array[1..2] of Extended;
The Arrays extensions/branches values can be anything you want:
begin
StringArray[1]:= 'Hello';
IntegerArray[1]:= 5;
BooleanArray[1]:= True;
ExtendedArray[1]:= 0.55;
end.
Multiple Arrays in one declaration:
If you wanted to do more than 1 different root arrays with exactly the same values, instead of doing:
var
Kane: array[1..2] of Integer;
Kane2: array[1..2] of Integer;
You can do:
var
Kane, Kane2: array[1..2] of Integer;
This will make both "Kane" and, "Kane2" seperate Integer arrays with a start point of 1 and an end point of 2. :)
Arrays in Procedures/Functions:
Just like Local variables, Arrays are able to be put into Procedures and Functions for their own private use in that area:
procedure FixMe;
var
Kane: array[1..2] of String;
begin
Kane[1]:= 'Hello';
Kane[2]:= 'Kane';
Writeln(Kane[1]+' '+Kane[2]);
end;
Function ResultMe: Boolean;
var
IsMe: array[1..2] of Boolean;
begin
IsMe[1]:= False;
IsMe[2]:= True;
if (IsMe[2] = True) then
Writeln('True!');
result:= IsMe[1];
end;
Using Arrays in other Functions/Procedures:
Like other Variables, arrays can be used in other Scar functions and procedures. Such as WriteLn, AddToReport, ect.
var
Words: array[1..3] of String;
begin
Words[1]:= 'Hello';
Words[2]:= 'You';
Words[3]:= 'Lol';
Writeln(Words[1]+' '+Words[2]+' '+Words[3]);
end.
It would be just like using a normal variable but it includes the Branch/Extensions number to determine which value to collect.
Comparing Arrays to using Normal Variables:
Arrays allow alot smoother, cleaner and easier coding.
Look at this example:
var
a, b, c, d, e, f, g: Integer;
begin
a:= 1;
b:= 2;
c:= 3;
d:= 4;
e:= 5;
g:= 6;
end.
To many variables. It can all be shortened down simply by:
var
Numbers: array[1..6] of Integer;
begin
Numbers[1]:= 1;
Numbers[2]:= 2;
Numbers[3]:= 3;
Numbers[4]:= 4;
Numbers[5]:= 5;
Numbers[6]:= 6;
end.
Numbers[1] would have been a, downthrough to Numbers[6] would have been g.
Using arrays with For:
For is a simple run through of values for an Integer from a start point to an End point:
var
i: Integer;
begin
for i:= 1 to 5 do
Writeln(inttostr(i));
end.
This would write from 1 to 5 because the Integer "i" is told to run from 1 to 5. If i had an Array of 100 extensions and wanted each extension to be a number upgraded from the last one, i could simply do:
var
Numbers: array[1..100] of Integer;
I: Integer;
begin
for i:= 1 to 100 do
Numbers[i]:= i;
end.
Which would mean that the number "i" was counted up to would be the Arrays branch/extensions value. It's alot easier than making 100 different Integer variables and going through a whole script with them plus there values.
Setarraylength and Getarraylength:
This is where Arrays can be even more handy! Arrays can have a start point but an Infiniate amount of end points. By this i mean that the through a script you can add another Extension or Branch to the root array which can have it's own value like any other Branches/Extensions.
They are declared differently to a normal array. You don't set a start or end point, just the name and the Arrays root variable:
var
Kane: array of Integer;
When you have done this you need to tell scar the arrays start point by using Setarraylength:
var
Kane: array of Integer;
begin
SetArraylength(Kane, 1);
end.
This would set my Array, Kane's value to "1". This would make my array able to be used:
var
Kane: array of Integer;
begin
SetArraylength(Kane, 1);
Kane[0]:= 5;
end.
I have a 0 when i set it to 1 because the second parametre in Setarraylength means its max value. Which means that that number minus 1 is the real value. Making it 1 would be:
var
Kane: array of Integer;
begin
SetArraylength(Kane, 1 + 1);
Kane[1]:= 5;
end.
If i hadn't added the extra "+ 1" and tryed to use the array, Kane's Branch/Extension "1". I would get a runtime/Out of range error:
[Runtime Error] : Out Of Range
If i was in a script and wanted to upgrade the Branches/Extensions of an array, I could simply do:
var
Kane: array of Integer;
begin
Setarraylength(Kane, 1);
Kane[0]:= 5;
Setarraylength(Kane, 2);
Kane[1]:= 10;
SetArrayLength(Kane, 3 + 1);
Kane[2]:= 13;
Kane[2]:= 5;
end.
Using Getarraylength simply returns with the current length of a Setarraylength accessible array:
var
Nothing: array of Integer;
begin
Setarraylength(Nothing, 5);
Writeln(inttostr(GetArrayLength(Nothing)));
end.
This would write, "5", because that's what i have set the array to. Now, i know before i mentioned that the arrays set value is minused by 1, Which is true. Getarraylength get's the max value of the array, so the real value would be:
var
Nothing: array of Integer;
begin
Setarraylength(Nothing, 5);
Writeln(inttostr(GetArrayLength(Nothing)-1));
end.
Which would write, "4", because i've minused the max by 1. :)
Heres a little script that uses most of what has been explained in this section:
var
Numbers: array of Integer;
begin
ClearDebug;
Setarraylength(Numbers, 1);
Numbers[0]:= 5;
if (Numbers[0] = 5) then
begin
Setarraylength(Numbers, 2);
end;
if (GetArrayLength(Numbers) = 2) then
begin
Numbers[1]:= 10;
end;
if (Numbers[1] = (Numbers[0] + (Numbers[1]/2))) then
begin
SetArrayLength(Numbers, 5);
end;
Writeln('Final Array Start point: '+inttostr(GetArrayLength(Numbers)));
Writeln('But since "Getarraylength" get''s the max value, It would really be: '+inttostr(GetArrayLength(Numbers)-1));
end.
Arrays may look hard, But they're actually very simple.
Concluding:
I hope you have learnt something new about arrays and how to use them. Arrays are extremely handy in various ways. They are Changable, User Set-able, Cleaner, More proffessional, ect.
If you have any more questions, Do NOT hesitate to ask in this thread. I will reply with an satisfactory answer.
Everything in this Tutorial was made by XxKanexX.
Arrays are excellent for taking place of those countless integers or making a variable that possibly can always be upgraded and another value set to it.
Arrays may seem hard, but once you know how to use them you will come to realise that they are extremely easy aswell as handy.
Explaining an Array:
An Array is somewhat like a never ending variable. It can be one root variable with however many user set values. Seem confusing? - Think of it as a Tree. All of the Array extensions are the branches. So if i had an Array named Numbers and it had a user set number of branches of 20, then there is 20 branches which can all have there own value.
The Array layout:
Arrays are first changed or called by their root name. Next is the number of the array extension. In this example, The arrays name is Numbers and the extension number is 2.
Numbers[2]:= Data;
If the Arrays name was Kane[/b] and the extension number was [i]5 then it would be:
Kane[5]:= Data;
The number in between the brackets is, like explained above, similar to a tree's branches.
Kane:= Data;
Kane:= Data;
Kane:= Data;
Each branch can have it's own data all installed into 1 root variable. :) It all saves alot of valuable time and lines. It can also keep the code looking clean and a little more proffesional.
Declaring an Array:
An array is declared like any other variable. Although it also has another part where you can set where the array starts, and ends.
In this example, the Arrays name is Numbers, It starts at 1 and Ends at 3.
[b]var
Numbers: Array[1..3] of Integer;
From the example above, I'll explain from left to right what everything means.
Numbers is the arrays name, it's followed by a Colon to explain to Scar that the name has ended and to continue with the variables values.
The array name along with a colon is followed by "Array", which tells Scar that this variable is an Array.
Most people can get confused by the next bit. It's a Start number followed by 2 dots then an End number in brackets. Since i have put "1..3" it means that the Array "Numbers" has 3 values:
Numbers[1]:= Data;
Numbers[2]:= Data;
Numbers[3]:= Data;
Just like "Numbers" is the Trees root and the numbers in between the brackets are the branches. The start and end number can be anything, As long as the Start is smaller than the end number. Each branch can have it's own value:
Numbers[1]:= 1;
Numbers[2]:= 50;
Numbers[3]:= 3;
"of" is simply the root variable of the Array. Which can be followed by whatever you want the array and it's extensions to be:
of Integer, String, Byte, Char, ect.
I have chosen Integer. Which means ever branch/extension of the arrays value is an Integer.
Values and Root Array Variables:
A root Array Variable is the Arrays branches/extensions variable value. Like a normal variable, You can set it to be a: [i]String, Integer, Byte, Char, Boolean, ect. This is the same with an Array. You can use any of the normal root Variable choices.
So if i had:
of Boolean;
It would mean that that Arrays extensions/branches are all booleans. Which means they can all be either True or False. If it was:
of String;
Then all the values could be Strings, which leads me to the next bit of the tutorial. Explaining how to set an Arrays value.
We'll use this Array as an example:
var
Numbers: array[1..3] of Integer;
Which, if you have read above, Means that my root array is Numbers, It's start is [i]1 and end is 3 And all the values are Integers.
Giving each Extension/branch a value:
Working with the example in the previous part of the tutorial, we need to set the Array Numbers's Branches/Extensions values. To do this we would do:
var
Numbers: array[1..3] of Integer;
begin
Numbers[1]:= 5;
Numbers[2]:= 6;
Numbers[3]:= 2;
end.
Above means that in the main loop i have set each Branch/Extensions value to whatever i want it to be, aslong as it's an integer as that's what the root variable of the Array is.
The data can be anything you want, Just like a normal variable.
If i were to make the root Array Variable a string i could do:
var
Names: array[1..4] of String;
begin
Names[1]:= 'Kane';
Names[2]:= 'Ben';
Names[3]:= 'Krazy';
Names[4]:= 'Pyro';
end.
The other ways of Arrays:
Here i'll just show you some scripts of all the possibilites of declaring an array and setting it's values.
The Start and End points can be anything you want:
var
Kane: array[3..6] of Integer;
Just aslong as the Start number was smaller than the end number.
Above i start at 3 and end at 6. This means my branches/extensions can be 3 - 6:
var
Kane: array[3..6] of Integer;
begin
Kane[3]:= 5;
Kane[4]:= 2;
Kane[5]:= 65;
Kane[6]:= 0;
end.
The name can be anything you want:
var
BlaLaFLO: array[0..6] of String;
The root array variable can be any variable:
var
Kane: array[1..2] of Integer;
var
Kane: array[1..2] of Boolean;
var
Kane: array[1..2] of String;
var
Kane: array[1..2] of Char;
var
Kane: array[1..2] of Extended;
The Arrays extensions/branches values can be anything you want:
begin
StringArray[1]:= 'Hello';
IntegerArray[1]:= 5;
BooleanArray[1]:= True;
ExtendedArray[1]:= 0.55;
end.
Multiple Arrays in one declaration:
If you wanted to do more than 1 different root arrays with exactly the same values, instead of doing:
var
Kane: array[1..2] of Integer;
Kane2: array[1..2] of Integer;
You can do:
var
Kane, Kane2: array[1..2] of Integer;
This will make both "Kane" and, "Kane2" seperate Integer arrays with a start point of 1 and an end point of 2. :)
Arrays in Procedures/Functions:
Just like Local variables, Arrays are able to be put into Procedures and Functions for their own private use in that area:
procedure FixMe;
var
Kane: array[1..2] of String;
begin
Kane[1]:= 'Hello';
Kane[2]:= 'Kane';
Writeln(Kane[1]+' '+Kane[2]);
end;
Function ResultMe: Boolean;
var
IsMe: array[1..2] of Boolean;
begin
IsMe[1]:= False;
IsMe[2]:= True;
if (IsMe[2] = True) then
Writeln('True!');
result:= IsMe[1];
end;
Using Arrays in other Functions/Procedures:
Like other Variables, arrays can be used in other Scar functions and procedures. Such as WriteLn, AddToReport, ect.
var
Words: array[1..3] of String;
begin
Words[1]:= 'Hello';
Words[2]:= 'You';
Words[3]:= 'Lol';
Writeln(Words[1]+' '+Words[2]+' '+Words[3]);
end.
It would be just like using a normal variable but it includes the Branch/Extensions number to determine which value to collect.
Comparing Arrays to using Normal Variables:
Arrays allow alot smoother, cleaner and easier coding.
Look at this example:
var
a, b, c, d, e, f, g: Integer;
begin
a:= 1;
b:= 2;
c:= 3;
d:= 4;
e:= 5;
g:= 6;
end.
To many variables. It can all be shortened down simply by:
var
Numbers: array[1..6] of Integer;
begin
Numbers[1]:= 1;
Numbers[2]:= 2;
Numbers[3]:= 3;
Numbers[4]:= 4;
Numbers[5]:= 5;
Numbers[6]:= 6;
end.
Numbers[1] would have been a, downthrough to Numbers[6] would have been g.
Using arrays with For:
For is a simple run through of values for an Integer from a start point to an End point:
var
i: Integer;
begin
for i:= 1 to 5 do
Writeln(inttostr(i));
end.
This would write from 1 to 5 because the Integer "i" is told to run from 1 to 5. If i had an Array of 100 extensions and wanted each extension to be a number upgraded from the last one, i could simply do:
var
Numbers: array[1..100] of Integer;
I: Integer;
begin
for i:= 1 to 100 do
Numbers[i]:= i;
end.
Which would mean that the number "i" was counted up to would be the Arrays branch/extensions value. It's alot easier than making 100 different Integer variables and going through a whole script with them plus there values.
Setarraylength and Getarraylength:
This is where Arrays can be even more handy! Arrays can have a start point but an Infiniate amount of end points. By this i mean that the through a script you can add another Extension or Branch to the root array which can have it's own value like any other Branches/Extensions.
They are declared differently to a normal array. You don't set a start or end point, just the name and the Arrays root variable:
var
Kane: array of Integer;
When you have done this you need to tell scar the arrays start point by using Setarraylength:
var
Kane: array of Integer;
begin
SetArraylength(Kane, 1);
end.
This would set my Array, Kane's value to "1". This would make my array able to be used:
var
Kane: array of Integer;
begin
SetArraylength(Kane, 1);
Kane[0]:= 5;
end.
I have a 0 when i set it to 1 because the second parametre in Setarraylength means its max value. Which means that that number minus 1 is the real value. Making it 1 would be:
var
Kane: array of Integer;
begin
SetArraylength(Kane, 1 + 1);
Kane[1]:= 5;
end.
If i hadn't added the extra "+ 1" and tryed to use the array, Kane's Branch/Extension "1". I would get a runtime/Out of range error:
[Runtime Error] : Out Of Range
If i was in a script and wanted to upgrade the Branches/Extensions of an array, I could simply do:
var
Kane: array of Integer;
begin
Setarraylength(Kane, 1);
Kane[0]:= 5;
Setarraylength(Kane, 2);
Kane[1]:= 10;
SetArrayLength(Kane, 3 + 1);
Kane[2]:= 13;
Kane[2]:= 5;
end.
Using Getarraylength simply returns with the current length of a Setarraylength accessible array:
var
Nothing: array of Integer;
begin
Setarraylength(Nothing, 5);
Writeln(inttostr(GetArrayLength(Nothing)));
end.
This would write, "5", because that's what i have set the array to. Now, i know before i mentioned that the arrays set value is minused by 1, Which is true. Getarraylength get's the max value of the array, so the real value would be:
var
Nothing: array of Integer;
begin
Setarraylength(Nothing, 5);
Writeln(inttostr(GetArrayLength(Nothing)-1));
end.
Which would write, "4", because i've minused the max by 1. :)
Heres a little script that uses most of what has been explained in this section:
var
Numbers: array of Integer;
begin
ClearDebug;
Setarraylength(Numbers, 1);
Numbers[0]:= 5;
if (Numbers[0] = 5) then
begin
Setarraylength(Numbers, 2);
end;
if (GetArrayLength(Numbers) = 2) then
begin
Numbers[1]:= 10;
end;
if (Numbers[1] = (Numbers[0] + (Numbers[1]/2))) then
begin
SetArrayLength(Numbers, 5);
end;
Writeln('Final Array Start point: '+inttostr(GetArrayLength(Numbers)));
Writeln('But since "Getarraylength" get''s the max value, It would really be: '+inttostr(GetArrayLength(Numbers)-1));
end.
Arrays may look hard, But they're actually very simple.
Concluding:
I hope you have learnt something new about arrays and how to use them. Arrays are extremely handy in various ways. They are Changable, User Set-able, Cleaner, More proffessional, ect.
If you have any more questions, Do NOT hesitate to ask in this thread. I will reply with an satisfactory answer.
Everything in this Tutorial was made by XxKanexX.