Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to combine 2 arrays of records (as a map) into a third array in Pascal

1 Answer

0 votes
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

*)

 



answered Aug 25, 2025 by avibootz
edited Aug 25, 2025 by avibootz
...