Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Arrays

  1. #1
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default Arrays

    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.
    Code:
    Numbers[2]:= Data;
    If the Arrays name was [i]Kane[/b] and the extension number was 5 then it would be:
    Code:
    Kane[5]:= Data;
    The number in between the brackets is, like explained above, similar to a tree's branches.
    Code:
    Kane[Branch1]:= Data;
    Kane[Branch2]:= Data;
    Kane[Branch3]:= 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.
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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[/b], ect. This is the same with an Array. You can use any of the normal root Variable choices.
    So if i had:
    Code:
    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:
    Code:
    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:
    Code:
    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[/b] 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:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    var
      BlaLaFLO: array[0..6] of String;
    The root array variable can be any variable:
    Code:
    var
      Kane: array[1..2] of Integer;
    Code:
    var
      Kane: array[1..2] of Boolean;
    Code:
    var
      Kane: array[1..2] of String;
    Code:
    var
      Kane: array[1..2] of Char;
    Code:
    var
      Kane: array[1..2] of Extended;
    The Arrays extensions/branches values can be anything you want:
    Code:
    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:
    Code:
    var
      Kane: array[1..2] of Integer;
      Kane2: array[1..2] of Integer;
    You can do:
    Code:
    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:
    Code:
    procedure FixMe;
    var
      Kane: array[1..2] of String;
    
    begin
      Kane[1]:= 'Hello';
      Kane[2]:= 'Kane';
       Writeln(Kane[1]+' '+Kane[2]);
    end;
    Code:
    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.
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    var
      Kane: array of Integer;
    When you have done this you need to tell scar the arrays start point by using Setarraylength:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    [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:
    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Reserved. ;D <3 SRL

  3. #3
    Join Date
    Feb 2006
    Location
    Pennsylvania
    Posts
    1,524
    Mentioned
    2 Post(s)
    Quoted
    1 Post(s)

    Default

    Very good job.

    Well put together.

  4. #4
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Bebemycat2
    Very good job.

    Well put together.
    Thankx heaps ;o It took me about 2 hours. -.-

  5. #5
    Join Date
    Feb 2006
    Posts
    582
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by XxKanexX
    Thankx heaps ;o It took me about 2 hours. -.-
    Kane, do you remember the time.. When you had a life?
    Free File Hosting
    No download timers!

    Rifkwtf.com

  6. #6
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Pwnd
    Kane, do you remember the time.. When you had a life?
    Vividly

  7. #7
    Join Date
    Feb 2006
    Posts
    582
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by XxKanexX
    Vividly
    Are you sure? I can't recall you ever having a life.
    Free File Hosting
    No download timers!

    Rifkwtf.com

  8. #8
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Pwnd
    Are you sure? I can't recall you ever having a life.
    -Opens up Book of Kanes Life-
    -Stares Blankly at the only 2 pages-
    -Frowns-

  9. #9
    Join Date
    Feb 2006
    Posts
    582
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by XxKanexX
    -Opens up Book of Kanes Life-
    -Stares Blankly at the only 2 pages-
    -Frowns-
    -Rips the pages out-
    -Kane disappears-
    =O
    Free File Hosting
    No download timers!

    Rifkwtf.com

  10. #10
    Join Date
    Mar 2006
    Posts
    509
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    looks good kane haha you should try figuring out multi-dimensional arrays god was that annoying

  11. #11
    Join Date
    Feb 2006
    Posts
    131
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Mutant Squirrle
    looks good kane haha you should try figuring out multi-dimensional arrays god was that annoying
    You forgot to say who saved the day...

  12. #12
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    I might make a TPointarray tutorial to go along with it later Although if you know arrays they're pretty much easy.

  13. #13
    Join Date
    Feb 2006
    Location
    Australia
    Posts
    628
    Mentioned
    15 Post(s)
    Quoted
    105 Post(s)

    Default

    You're very detailed kane, great to see your tutorial
    I'm sure you've saved lots of people the frustation and time of explaining arrays <3

  14. #14
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Krazy_Meerkat
    You're very detailed kane, great to see your tutorial
    I'm sure you've saved lots of people the frustation and time of explaining arrays <3
    If i hadn't lost all myne at Kaitnieks there would be around 5 or more tutorials here ;(

  15. #15
    Join Date
    Feb 2006
    Location
    Franklin, Ohio, USA
    Posts
    991
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    isnt there some tarray or somthing thats dynamic? because I need dynamic arrays like in java

  16. #16
    Join Date
    Feb 2006
    Location
    Aussie
    Posts
    937
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    also kane, maybe add a bit to this explaining the ramdom array thing you use, like how you got a list of array's then scar will randomly select one and do whatever it does with it (i.e. sends the text) that would be awesome

  17. #17
    Join Date
    Feb 2006
    Location
    L.A, USA
    Posts
    1,632
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I never used an array before, except once while making a random chat sender.

  18. #18
    Join Date
    Feb 2006
    Posts
    406
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by moparisthebest
    isnt there some tarray or somthing thats dynamic? because I need dynamic arrays like in java
    yeah, just dont set a length at the beginning
    like

    var
    test:array of integer;

    it starts with zero length, so you need to use

    setarraylength(array,length)

    or you'll get range errors

  19. #19
    Join Date
    Mar 2006
    Posts
    509
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    ^^ you learn something new everyday -_-

  20. #20
    Join Date
    Feb 2006
    Location
    Berkeley, CA
    Posts
    1,837
    Mentioned
    52 Post(s)
    Quoted
    60 Post(s)

    Default

    I thought everyone knew how to use arrays Anyway, someone write a tutorial on types so people understand the player array more

  21. #21
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    485
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    VERY nice Kane, Starblaster posted all your tutorials on Saradomin for awhile before it died again.

  22. #22
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by The Prince of Randomness
    VERY nice Kane, Starblaster posted all your tutorials on Saradomin for awhile before it died again.
    He did? I never noticed. I found all of my old ones somewhere and posted them here So let's hope they come in use.

  23. #23
    Join Date
    Jul 2008
    Location
    Berlin, Wisconsin. A small town if I say so myself.
    Posts
    10
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    A decent tutorial. Learned something new. Good job on the tutorial.

  24. #24
    Join Date
    Feb 2008
    Posts
    0
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    tuts r helping meh ty

  25. #25
    Join Date
    Feb 2008
    Posts
    0
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    lllllllllllllllol you should be proud all this helped me

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Arrays, stuck on arrays
    By Camaro' in forum OSR Help
    Replies: 1
    Last Post: 03-08-2008, 02:02 AM
  2. Need Help With Arrays
    By papenco in forum OSR Help
    Replies: 16
    Last Post: 11-28-2007, 03:56 AM
  3. Help with Arrays
    By kooldude in forum OSR Help
    Replies: 16
    Last Post: 06-15-2007, 05:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •