Results 1 to 13 of 13

Thread: Having fun with Arrays!

  1. #1
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default Having fun with Arrays!

    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
    1. What is an array
    2. What are arrays useful for
    3. Declaring an array
    4. Initializing an array
    5. Giving an array index a value
    6. For..to..do loops and arrays
    7. ATPAs and TPAs + for loops
    8. 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.

    Simba Code:
    //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:


    Although the script will compile, the IDE will receive a Runtime Error.

    You can circumvent these by doing the following:

    Simba Code:
    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!

    Simba Code:
    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:


    Simba Code:
    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:

    Simba Code:
    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:

    Code:
    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:


    Simba Code:
    if FindColorsSpiralTolerance(P.x,P.y,TPA,Color,3,3,518,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

    Simba Code:
    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!

  2. #2
    Join Date
    Mar 2006
    Location
    Belgium
    Posts
    3,564
    Mentioned
    111 Post(s)
    Quoted
    1475 Post(s)

    Default

    Looks useful gonna read this thru.

    Creds to DannyRS for this wonderful sig!

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    I needed this after that skype convo we had

  4. #4
    Join Date
    Nov 2011
    Location
    Sacramento, California
    Posts
    366
    Mentioned
    4 Post(s)
    Quoted
    85 Post(s)

    Default

    Gonna need this after an IRC convo I had...

  5. #5
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Awesome, hope its useful

  6. #6
    Join Date
    Nov 2011
    Location
    Sacramento, California
    Posts
    366
    Mentioned
    4 Post(s)
    Quoted
    85 Post(s)

    Default

    Quote Originally Posted by Sin View Post


    Simba Code:
    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

  7. #7
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Quote Originally Posted by sahibjs View Post
    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)]

  8. #8
    Join Date
    Nov 2011
    Location
    Sacramento, California
    Posts
    366
    Mentioned
    4 Post(s)
    Quoted
    85 Post(s)

    Default

    Quote Originally Posted by Sin View Post
    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?

  9. #9
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    show something about matching array strings
    Simba Code:
    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.

  10. #10
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    @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
    Last edited by xtrapsp; 04-04-2013 at 10:37 AM.

  11. #11
    Join Date
    Jun 2007
    Location
    south park
    Posts
    1,160
    Mentioned
    0 Post(s)
    Quoted
    62 Post(s)

    Default

    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
    http://www.youtube.com/user/YoHoJoSRL
    Good scripting guides on youtube
    Formerly known as (djcheater)

  12. #12
    Join Date
    Aug 2012
    Location
    The Dark Tower
    Posts
    154
    Mentioned
    5 Post(s)
    Quoted
    56 Post(s)

    Default

    Thanks for the guide man.
    Helping me further my understanding of Arrays.

  13. #13
    Join Date
    Jul 2015
    Posts
    80
    Mentioned
    1 Post(s)
    Quoted
    22 Post(s)

    Default

    Used it today, when i needed to check worlds, thanks!

Thread Information

Users Browsing this Thread

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

Posting Permissions

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