954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Procedure for deleting array content

Hello, guys!

I have to make a program with the following features.
- array of records

TYPE
  employeeRecord  = RECORD
                   name: STRING;
                   surname: STRING;
                   age: Integer;
                   END;
  employeeRecords = ARRAY OF employeeRecord ;


-a procedure/function to ADD data into the array
I did this one and I think it is working fine!
- a procedure/function to DELETE data from the array
I have problems with this procedure. It doesn't work when I try to delete the data in the first position of the array. Here's what I did so far:

PROCEDURE delete(VAR records: employeeRecords);
VAR
  n, j: Integer;
  nameDel: STRING;

BEGIN
  Write('Name: ');
  Readln(nameDel);

  n := 0;

  WHILE (records[n].name <> nameDel) AND (n < High(records)) DO
  BEGIN
    inc(n);
  END;
 
  IF records[n].name <> nameDel THEN
  BEGIN
    Writeln('That employee could not be found!');
    Writeln;
  END;

  IF records[n].name = nameDel THEN
  BEGIN
    FOR j := n TO High(records)-1 DO
    BEGIN
      records[j].name := records[j+1].name;
      records[j].surname := records[j+1].surname;
      records[j].age:= records[j+1].age;
      SetLength(records, High(records)); // MAYBE THIS LINE IS NOT CORRECT
    END
  END

END;


If you have some ideas, please let me know. Thanks in advance!

simps0n
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

The SetLength should not be in the loop, but after it. Other than that it looks fine to me.

BTW. You should be able to replace lines 27..29 with just
records[j] := records[j+1];

Don't know why you are using all-caps keywords, but it sure is anachronistic (a hold-over from brain-damaged BASIC users). I've not changed the functionality of your code here. Just structured it a little better...

PROCEDURE delete(VAR records: employeeRecords);
VAR
  n, j: Integer;
  nameDel: String;

BEGIN
  Write('Name: ');
  Readln(nameDel);

  n := 0;

  WHILE (records[n].name <> nameDel) AND (n < High(records)) DO
    Inc(n);
 
  IF records[n].name = nameDel
    THEN BEGIN
           FOR j := n TO High(records)-1 DO
             records[j] := records[j+1];
           SetLength(records, High(records))
         END
    ELSE BEGIN
           Writeln('That employee could not be found!');
           Writeln
         END
END;

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Thank you very much, Duoas! Much appreciated.
You were perfectly clear and really helped me a lot.

Don't know why you are using all-caps keywords, ...

I'm sorry for that.

simps0n
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You