DeleteRecord |
Databases | |
Aptilis 1 |
DeleteRecord(Database[], Key, Position, n[, startFrom])
DeleteRecord will scan a database and remove each record that has
a field matching the given Key at the given Position.
In database parlance, the Position of the key is it's column.
It will remove up to 'n' records or as many it finds that
match the request. If you specify a value of 1 for n, then
only the first matching record will be erased.
You can use the extra 'startFrom' parameter to skip the
first 'startFrom' records of the database.
Return value:
The number of records removed.
Example 1:
We remove all matching records. "mammal" will be found in column
'3', the French name has an index of '0' and so on.
sub main db[0] = makeRecord("chien","dog","1","mammal") $ db[1] = makeRecord("chat","cat","2","mammal") $ db[2] = makeRecord("poisson","fish","3","vertebrate") $ db[3] = makeRecord("escargot","snail","4","invertebrate") $ db[4] = makeRecord("souris","mouse","5","mammal") $ db[5] = makeRecord("perroquet","parrot","6","bird") $ db[6] = makeRecord("dauphin","dolphin","7","mammal") $ db[7] = makeRecord("fleur","flower","8","plant") $ n = getArraySize(db[]) deleteRecord(db[], "mammal", 3, n) n = getArraySize(db[]) print("New size: ", n, "\n") for i=0 to n-1 getAllFields(fl[], db[i]$) print(fl[1]$, " ", fl[3]$, "\n") end for end subResult:
New size: 4.000000 fish vertebrate snail invertebrate parrot bird flower plant |
Example 2:
Here, we specify to remove only one field, so only poor doggy
will go. :-(
sub main db[0] = makeRecord("chien","dog","1","mammal") $ db[1] = makeRecord("chat","cat","2","mammal") $ db[2] = makeRecord("poisson","fish","3","vertebrate") $ db[3] = makeRecord("escargot","snail","4","invertebrate") $ db[4] = makeRecord("souris","mouse","5","mammal") $ db[5] = makeRecord("perroquet","parrot","6","bird") $ db[6] = makeRecord("dauphin","dolphin","7","mammal") $ db[7] = makeRecord("fleur","flower","8","plant") $ n = getArraySize(db[]) deleteRecord(db[], "mammal", 3, 1) n = getArraySize(db[]) print("New size: ", n, "\n") for i=0 to n-1 getAllFields(fl[], db[i]$) print(fl[1]$, " ", fl[3]$, "\n") end for end subResult:
New size: 7.000000 cat mammal fish vertebrate snail invertebrate mouse mammal parrot bird dolphin mammal flower plant |
Example 3:
Lastly, we specify to remove as many records as possible, however,
we start at positon 2, hence saving both dog and cat from oblivion.
sub main db[0] = makeRecord("chien","dog","1","mammal") $ db[1] = makeRecord("chat","cat","2","mammal") $ db[2] = makeRecord("poisson","fish","3","vertebrate") $ db[3] = makeRecord("escargot","snail","4","invertebrate") $ db[4] = makeRecord("souris","mouse","5","mammal") $ db[5] = makeRecord("perroquet","parrot","6","bird") $ db[6] = makeRecord("dauphin","dolphin","7","mammal") $ db[7] = makeRecord("fleur","flower","8","plant") $ n = getArraySize(db[]) deleteRecord(db[], "mammal", 3, n, 2) n = getArraySize(db[]) print("New size: ", n, "\n") for i=0 to n-1 getAllFields(fl[], db[i]$) print(fl[1]$, " ", fl[3]$, "\n") end for end subResult:
New size: 6.000000 dog mammal cat mammal fish vertebrate snail invertebrate parrot bird flower plant |