Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-12

Map of values to array of records

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jiří Pavlovský

Hello,

how can I create two level map where value is array of records? These records are constant - I don't need to compute them or anything. I'm quite new to pascal and for gods sake cannot figure it out. I tried to use TFPGMap for that.

I mean somewhere in the code I get two values and based on these I need to get those records.

my dream would be something like
myArrayOfRecords := myMap[some_value][other_value];

I even tried to write a function that would return array of records based on its parameters, but that does not work either. I cannot figure out how to assign the record.

This causes a compile error:
res[0] := (Field: 1; Empty: true; Check: false);

OTOH similar construct works in "const" section. So I'm quite puzzled.

Thank you

ik
to Jiří Pavlovský

2011/12/25 Jiří Pavlovský

Show quoted text 13 lines
Hello, how can I create two level map where value is array of records? These records are constant - I don't need to compute them or anything. I'm quite new to pascal and for gods sake cannot figure it out. I tried to use TFPGMap for that. I mean somewhere in the code I get two values and based on these I need to get those records. my dream would be something like myArrayOfRecords := myMap[some_value][other_value]**;
type
   TMyRec = record
     Field   : integer;
     Empty,
    Check : Boolean;
   end;
 TMyMap = array[0..Something] of TMyRec;
Show quoted text 9 lines
I even tried to write a function that would return array of records based on its parameters, but that does not work either. I cannot figure out how to assign the record. This causes a compile error: res[0] := (Field: 1; Empty: true; Check: false);
 res[0].Field := 1;
 res[0].Empty := true;
 res[0].Check := false;

if you want to make it as a function:

function ToMyRec (AField : Integer; AEmpty, ACheck : Boolean) : TMyRec;
begin
   Result.Field := AFiled;
   Result.Empty :=  AEmpty;
   Result.Check := AChek;
end;
res[0] := ToMyRec(...);
OTOH similar construct works in "const" section. So I'm quite puzzled.

What do you mean ?

Show quoted text 7 lines
Thank you ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Jiří Pavlovský
to ik
ik wrote 25 lines
2011/12/25 Jiří Pavlovský < > Hello, how can I create two level map where value is array of records? These records are constant - I don't need to compute them or anything. I'm quite new to pascal and for gods sake cannot figure it out. I tried to use TFPGMap for that. I mean somewhere in the code I get two values and based on these I need to get those records. my dream would be something like myArrayOfRecords := myMap[some_value][other_value]; type TMyRec = record Field : integer; Empty, Check : Boolean; end; TMyMap = array[0..Something] of TMyRec;

Thanks, but that is not want I need.
I don't want array, but a two level map where keys are not necessarily integers.

Show quoted text 7 lines
OTOH similar construct works in "const" section. So I'm quite puzzled. What do you mean ?

I mean the code below works ok, so I don't undestand why it doesn't compile when I use similar construct inside a function. const ranges: array [1..2] of TMyRec =
(
(Start: 0; Stop: 3),
(Start: 4; Stop: 7)

        );
Howard Page-Clark
to Jiří Pavlovský
Jiří Pavlovský wrote 8 lines
> What do you mean ? I mean the code below works ok, so I don't undestand why it doesn't compile when I use similar construct inside a function. const ranges: array [1..2] of TMyRec = ( (Start: 0; Stop: 3), (Start: 4; Stop: 7) );

Are you looking for something like this? (BTW your code snippets are too short to really understand what you can't do).

type
   TMyRec = record
     Start, Stop: integer;
   end;
TRange = array [1..2] of TMyRec;

const RANGES: TRange =
(
(Start: 0; Stop: 3),
(Start: 4; Stop: 7)

        );
function GetRec(anIndex: integer): TMyRec;
begin
   result := RANGES[anIndex];
end;
Jiří Pavlovský
to Howard Page-Clark
Howard Page-Clark wrote 13 lines
On 25/12/11 10:07, Jiří Pavlovský wrote: >> What do you mean ? > I mean the code below works ok, so I don't undestand why it doesn't > compile when I use similar construct inside a function. > const ranges: array [1..2] of TMyRec = > ( > (Start: 0; Stop: 3), > (Start: 4; Stop: 7) > ); Are you looking for something like this? (BTW your code snippets are too short to really understand what you can't do).

Yes, sorry I should have been more clear. It was late evening and I got frustrated...

I now got what I need working. Still if there is a way to make it more concise, Please let me know.

{$mode objfpc}
uses  fgl;
type
TMyRec = record
     Start, Stop : integer;
     End;
MyIntType = byte;
TArrayType = array of TMyRec;
TSubMyMap = specialize TFPGMap<Char, TArrayType>;
TMyMap = specialize TFPGMap<Char,TSubMyMap>;
var t : TMyMap;
  r : TMyRec;
  myArray : TArrayType;
begin
r.Start := 1; r.Stop := 2;
SetLength(myArray, 1);
myArray[0] := r;
t:= TMyMap.Create;
t['a'] := TSubMyMap.Create;
t['a']['b'] := myArray;
writeln(t['a']['b'][0].Start);
end.