Board index » delphi » Deleting a record from an array

Deleting a record from an array

In article <19990315192348.24197.00000...@ngol05.aol.com>,
apgour...@aol.com says...

Quote
> At the moment a have array set up as type record with four elements within the
> record. These elements are indexed by the variable "cell". I have no problem
> searching the array for any input and thus finding the element if it exists.
> The problem comes when I have to delete a record i.e on a search with an input
> related toName[cell], I can find this name and I have an Idea how to rewrite
> all the records after it over the one that needs to be deleted. Here is a rough
> outline of the record that I have set up.

> Type  phone_number = record
>                        name : string[12];
>                        age   : integer;
>                        number: string[12];
>                      end;

> I hope this makes sense to anyone out there.Each record is accessed by the
> integer variable cell when finding a record within a loop.

> I would appreciate any help on this problem. I am almost there but I think I
> have a logic problem, what's new about that, that could be my life story.

   There are several ways to "delete" array elements:
  1. Add a field which indicates the state of the record: "Present" or
"Deleted".  This can be a boolean, character, whatever.
  2. Use the Move procedure to shift the records.
  3. Assign the fields of the deleted element to "null" values.
   The difficulty here is that you don't explain what "delete" means -
physical removal, making the element "dead", shifting records, etc.  With
that information, we could be more specific...
 

Re:Deleting a record from an array


At the moment a have array set up as type record with four elements within the
record. These elements are indexed by the variable "cell". I have no problem
searching the array for any input and thus finding the element if it exists.
The problem comes when I have to delete a record i.e on a search with an input
related toName[cell], I can find this name and I have an Idea how to rewrite
all the records after it over the one that needs to be deleted. Here is a rough
outline of the record that I have set up.

Type
       phone_number = record
                                 name : string[12];
                                 age   : integer;
                                 number: string[12];
                                 end;

I hope this makes sense to anyone out there.Each record is accessed by the
integer variable cell when finding a record within a loop.

I would appreciate any help on this problem. I am almost there but I think I
have a logic problem, what's new about that, that could be my life story.
Allan Gourlay

Re:Deleting a record from an array


Right, here we go, it's a bit clunky is this method and requires all of the
records to be present in the array.

Search for the required key-field, and store it's position as say 'z'

then have a for loop say, for i:=1 to numrecords do

in this loop write:

recarray[z+count-1]:=recarray[z+count]

and that should work, I have a fully working database if you want it that
uses all of the searching, adding and deleting procedures. If you wnat it,
mail me.

James

In article <19990315192348.24197.00000...@ngol05.aol.com>,
  apgour...@aol.com (APGourlay) wrote:

Quote
> At the moment a have array set up as type record with four elements within the
> record. These elements are indexed by the variable "cell". I have no problem
> searching the array for any input and thus finding the element if it exists.
> The problem comes when I have to delete a record i.e on a search with an input
> related toName[cell], I can find this name and I have an Idea how to rewrite
> all the records after it over the one that needs to be deleted. Here is a rough
> outline of the record that I have set up.

> Type
>        phone_number = record
>                                  name : string[12];
>                                  age   : integer;
>                                  number: string[12];
>                                  end;

> I hope this makes sense to anyone out there.Each record is accessed by the
> integer variable cell when finding a record within a loop.

> I would appreciate any help on this problem. I am almost there but I think I
> have a logic problem, what's new about that, that could be my life story.
> Allan Gourlay

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Deleting a record from an array


Quote
APGourlay wrote in message <19990315192348.24197.00000...@ngol05.aol.com>...
>The problem comes when I have to delete a record i.e on a search with an
input
>related toName[cell], I can find this name and I have an Idea how to
rewrite
>all the records after it over the one that needs to be deleted. Here is a
rough
>outline of the record that I have set up.

>Type
>       phone_number = record
>                                 name : string[12];
>                                 age   : integer;
>                                 number: string[12];
>                                 end;

>I hope this makes sense to anyone out there.Each record is accessed by the
>integer variable cell when finding a record within a loop.

I suggest the following approach: -

1.)  Modify the record structure as follows: -
    Type
    phone_number = record
                               name : string[12];
                               age   : integer;
                               number: string[12];
                               deleteflag:boolean;
                             end;

2.)    Create the records with deleteflag initialised to false

3.)    To delete a record search for the relevant cell index

4.)    Set the delete flag to false: -    phone_number[cell].deleteflag:=
false;

5.)    Loop through all the records, only storing the cells in the array of
records where NOT(deleteflag)
        back to the array.

6.)     The last (n) records of the array need to be reinintialised to blank
records, where N = the number of records deleted.  Deleteflag needs to be
reset to false in all cases.

John

Re:Deleting a record from an array


Quote
John Kreeger wrote in message <7cr16c$2u...@aquila.mdx.ac.uk>...

>4.)    Set the delete flag to false: -    phone_number[cell].deleteflag:=
>false;

WHOOPS!  I got distracted by someone coming in here.
SET IT to True: -
phone_number[cell].deleteflag:= true {mark record for deletion}

Other Threads