SRL Archive - OSR Help

Array inside an array


Master BAW

2012-06-17 16:56:45

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]
<?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'));


?>
[/php]

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.

masterBB

2012-06-17 17:01:58

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

edit:

made the code a bit more complete:

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.

superuser

2012-06-17 17:05:02

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


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

...
end;

var
items = array of TItem;

...

SetLength(items, 1);

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

Abu

2012-06-17 17:05:16

Check out the tutorial on records in my signature.


EDIT: So like this:

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;

putonajonny

2012-06-17 17:11:49

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

Var
Objects : array of TObject;
then you can store your info:
SetArrayLength(Objects, 3);
Objects[0] := Object(30, 20, 10, 'carrot', 'ewbfuwerofbgerbgv');
Objects[1] := //...
or
Objects := [Object(30, 20, 10, 'carrot', 'ewbfuwerofbgerbgv'), Object(40, 2420, 2420, 'stick', 'esdvgisdfnsdvfbnofbgerbgv')...];

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

Master BAW

2012-06-17 17:12:01

Thanks all! :D You're a great help :)

Brandon

2012-06-17 17:12:43

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.


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

masterBB

2012-06-17 17:17:19

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

Brandon

2012-06-17 17:18:43

From: masterBB
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 :spongebob:

Master BAW

2012-06-17 17:22:22

haha :D +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!:D