program CombineMapsDemo;
type
TMapEntry = record
Key: string;
Value: string;
end;
const
MaxSize = 64;
var
Map1, Map2, Combined: array[1..MaxSize] of TMapEntry;
Size1, Size2, Size_Combined: integer;
i, j: integer;
found: boolean;
procedure InitializeMaps;
begin
// Initialize Map1
Size1 := 2;
Map1[1].Key := 'A'; Map1[1].Value := 'AAA';
Map1[2].Key := 'B'; Map1[2].Value := 'BBB';
// Initialize Map2
Size2 := 2;
Map2[1].Key := 'C'; Map2[1].Value := 'CCC';
Map2[2].Key := 'B'; Map2[2].Value := 'XYZ'; // Overwrites key 'B'
Map2[2].Key := 'D'; Map2[2].Value := 'DDD';
end;
procedure CombineMaps;
begin
Size_Combined := 0;
// Copy Map1 into Combined
for i := 1 to Size1 do
begin
inc(Size_Combined);
Combined[Size_Combined] := Map1[i];
end;
// Merge Map2 into Combined, overwriting duplicates
for i := 1 to Size2 do
begin
found := false;
for j := 1 to Size_Combined do
begin
if Combined[j].Key = Map2[i].Key then
begin
Combined[j].Value := Map2[i].Value; // Overwrite value
found := true;
break;
end;
end;
if not found then
begin
inc(Size_Combined);
Combined[Size_Combined] := Map2[i];
end;
end;
end;
procedure DisplayCombined;
begin
writeln('Combined Arrays:');
for i := 1 to Size_Combined do
writeln(Combined[i].Key, ' => ', Combined[i].Value);
end;
begin
InitializeMaps;
CombineMaps;
DisplayCombined;
end.
(*
run:
Combined Arrays:
A => AAA
B => BBB
C => CCC
D => DDD
*)