If I had a list of records that all used the same basic structure, and I wanted to be able to search through them and find records matching a certain filter pattern, the first thing I would do is put it into a SQL database instead of a file of [record type]
. Then I could simply run a query: select * from BOOKS where TITLE = :title
and let the DBMS handle the details.
If you can't do that, there are two basic ways to run your own filtering: by linear search, and by key lookup.
They both start the same way: read the entire file into memory, into an array of TBook. What you do next depends on your strategy.
Linear search is very simple:
function FindBookIndex(const searchTitle: string): integer;
begin
for i := 0 to high(bookArray) do
if bookArray[i].Title = searchTitle then
exit(i);
result := -1; //not found
end;
Indexed search takes a bit more work up-front, but is a lot faster to actually run the search. You set up a TDictionary
mapping strings (titles) to integers (array indices). Then to run a lookup, call TryGetValue
on the dictionary, and return -1 if there's no result. Note that this will only work on exact matches; if you want to do a partial match (books where the title contains the word "Wind," for example,) you either need a linear search or a much more sophisitcated indexing scheme.
But again, really the simplest way to do this is to put it in a database.