Results 1 to 10 of 10

Thread: Array inside an array

  1. #1
    Join Date
    Jun 2012
    Location
    THE Students-City of Holland
    Posts
    332
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default Array inside an array

    Hey,

    I want to make an advanced script where I need to have multiple information of multiple items. If you put it in a PHP-array (I have scripted PHP for 3 years, but am new to Pascal) it would look like this:

    PHP Code:
    <?php

    $Items 
    = array(array('name'=>'crayfish',
    'price'=> 15,
    'timeA'=> 32432522,
    'timeB'=> 88203221,
    'DMT' => 'jkeajwkfjlkewjkljklfheowif23fa8fdfahfkewjklfej'),
    array(
    'name'=>'Raw shark',
    'price'=> 1500,
    'timeA'=> 32442522,
    'timeB'=> 88333221,
    'DMT' => 'jkeajwkfjlkewjkljklfheowif23fa8fdfahfkewjklfej'),
    array(
    'name'=>'fire rune',
    'price'=> 20,
    'timeA'=> 31432522,
    'timeB'=> 22203221,
    'DMT' => 'jkeajwkfjlkewjkljklfheowif23fa8fdfahfkewjklfej'),
    array(
    'name'=>'gold ore',
    'price'=> 105,
    'timeA'=> 31432522,
    'timeB'=> 82203221,
    'DMT' => 'jkeajwkfjlkewjkljklfheowif23fa8fdfahfkewjklfej'));


    ?>
    But I need something like this in pascal. How the hell do I do this. I hope it's possible, because I need users to be able to add new items.

    I have read a couple of tutorials on arrays, but can't find my awnser inthere.

  2. #2
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Simba Code:
    type
      object = record
        price, timeA, timeB: Integer;
        name, DTM: String;

    edit:

    made the code a bit more complete:
    Simba Code:
    program new;

    type
      yourObject = record
        price, timeA, timeB: Integer;
        name, DTM: String;
      end;

    var
      yourArray: Array of yourObject;

    begin
      setLength(yourArray,2);
      yourArray[0].name := 'crayfish';
      yourArray[0].price := 15;
      yourArray[0].timeA := 32432522;
      yourArray[0].timeB := 88203221;
      yourArray[0].DTM := 'jkeajwkfjlkewjkljklfheowif23fa8fdfahfkewjklfej';

      yourArray[1].name := 'Raw shark';
      yourArray[1].price := 1500;
      yourArray[1].timeA := 32442522;
      yourArray[1].timeB := 88203221;
      yourArray[1].DTM := 'jkeajwkfjlkewjkljklfheowif23fa8fdfahfkewjklfej';
    end.
    Last edited by masterBB; 06-17-2012 at 09:06 PM.
    Working on: Tithe Farmer

  3. #3
    Join Date
    May 2007
    Posts
    527
    Mentioned
    12 Post(s)
    Quoted
    109 Post(s)

    Default

    Maybe something like this, as PascalScript has no associative arrays:

    Simba Code:
    type
        TItem = record
            name: string;
            price: integer;

            ...
        end;

    var
        items = array of TItem;

    ...

    SetLength(items, 1);

    items[0].name := 'Foo';

  4. #4
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Check out the tutorial on records in my signature.


    EDIT: So like this:

    Simba Code:
    type ItemInfo = record //called ItemInfo because well, it holds Item Info
      Price, TimeA, TimeB, DTM: Integer;  //the variables we want
    end;

    var
      Item: ItemInfo;  // 'Item' will be used to represent ItemInfo

    procedure LoadItemInfo; // See how it fits it's purpose?
    begin
      case lowercase(Players[CurrentPlayer].Strings[0]) of //you can make it a case of anything
        'crayfish':    begin
                         Item.Price := 0;
                         Item.TimeA := 0
                         Item.TimeB := 0
                         Item.DTM := DTMFromString('Make your own DTM!');
                       end;

        'raw shark':   begin
                         Item.Price := 0;
                         Item.TimeA := 0
                         Item.TimeB := 0
                         Item.DTM := DTMFromString('Make your own DTM!');
                       end;

        'fire rune':   begin
                         Item.Price := 0;
                         Item.TimeA := 0
                         Item.TimeB := 0
                         Item.DTM := DTMFromString('Make your own DTM!');
                       end;

        'gold ore':    begin
                         Item.Price := 0;
                         Item.TimeA := 0
                         Item.TimeB := 0
                         Item.DTM := DTMFromString('Make your own DTM!');
                       end;
      end;
    end;
    Last edited by Abu; 06-17-2012 at 09:19 PM.

  5. #5
    Join Date
    Oct 2011
    Location
    UK
    Posts
    1,322
    Mentioned
    2 Post(s)
    Quoted
    1 Post(s)

    Default

    If I take what masterBB said and expand on it: (I wrote this before I saw Abu's post, the only bit that is new is the function)
    Simba Code:
    type
      TObject = record
        price, timeA, timeB: Integer;
        name, DTM: String;
    end;//Don't forget the end

    This would be the record where you would store the info, you then need a function to neatly input the info into the record:
    Simba Code:
    Function Object(ObjPrice, ObjTimeA, ObjTimeB : integer; Objname, ObjDTM : String) : TObject;
    begin
      with Result do
        begin
          Price := ObjPrice;
          timeA := ObjTimeA;
          timeB := ObjTimeB;
          name := ObjName;
          DTM := ObjDTM;
        end;
    end;
    Then you need your variable to store the info in:
    Simba Code:
    Var
      Objects : array of TObject;
    then you can store your info:
    Simba Code:
    SetArrayLength(Objects, 3);
    Objects[0] := Object(30, 20, 10, 'carrot', 'ewbfuwerofbgerbgv');
    Objects[1] := //...
    or
    Simba Code:
    Objects := [Object(30, 20, 10, 'carrot', 'ewbfuwerofbgerbgv'), Object(40, 2420, 2420, 'stick', 'esdvgisdfnsdvfbnofbgerbgv')...];

    and you retrieve the info by doing:
    Simba Code:
    WriteLn(Objects[0].Price);

  6. #6
    Join Date
    Jun 2012
    Location
    THE Students-City of Holland
    Posts
    332
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default

    Thanks all! You're a great help

  7. #7
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    You mean like std::map? I don't know Php all that well either but here are possible solutions:

    I wrote out two solutions. The first uses a class to hold the data and I declared an array of that class.
    The second uses Dimensional Arrays. This method may possibly be the most difficult as you'd be creating arrays of the N'th dimension. Stick to whichever concept you figure is easiest.

    Simba Code:
    type
      Map = Record
      Name, Data: Variant;
    end;

    Function InputData: Array Of Map;
    begin
      SetLength(Result, 4);
      Result[0].Name:= 'GoldOre';
      Result[0].Data:= 105;

      Result[1].Name:= 'CrayFish';
      Result[1].Data:= 15;

      Result[2].Name:= 'RawFish';
      Result[2].Data:= 1500;

      Result[3].Name:= 'FireRune';
      Result[3].Data:= 20;
    end;

    //OR YOU CAN DO THIS:

    type
      T2DVariantArray = array of TVariantArray;

    Function DataHolder: T2DVariantArray;
    begin
      SetLength(Result[0], 4);
      SetLength(Result[1], 4);

      Result[0][0]:= 'GoldOre';
      Result[0][1]:= 'CrayFish';
      Result[0][2]:= 'RawShark';
      Result[0][3]:= 'FireRune';

      Result[1][0]:= 105;     //Gold Ore Price.
      Result[1][1]:= 15;      //Cray Fish Price.
      Result[1][2]:= 1500;    //RawShark Price.
      Result[1][3]:= 20;      //FireRune Price.
    end;


    EDIT: Got ninja'd bad.. lol GF me. Ahh I see everyone else used specific data-types.. I was thinking PHP style and used typeless containers. TVariants are typeless and can hold ALMOST any data type :P
    Last edited by Brandon; 06-17-2012 at 09:17 PM.
    I am Ggzz..
    Hackintosher

  8. #8
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Lol, when someones ask a well formulated question within the knowledge of the majority of the members, everyone ninjas everyone :P.
    Working on: Tithe Farmer

  9. #9
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    Lol, when someones ask a well formulated question within the knowledge of the majority of the members, everyone ninjas everyone :P.

    LOL yeah :P we each put different solutions but I was thinking PHP style and used Variants
    I am Ggzz..
    Hackintosher

  10. #10
    Join Date
    Jun 2012
    Location
    THE Students-City of Holland
    Posts
    332
    Mentioned
    0 Post(s)
    Quoted
    4 Post(s)

    Default

    haha +repped everyone
    Now I only understand half of it, but I'll read some tutorials on types and record-types.
    this helps me a lot!

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
  •