K
K
Kezzya2020-05-16 21:45:18
Delphi
Kezzya, 2020-05-16 21:45:18

How to parse a text editor in delphi?

There is a textbook, there are book titles, authors, and the number of pages. It is necessary to load the data from the textbook into an array of records. The idea is this, I save the file into a variable with the ShortString type, I run through the text and when there is a space, then there is another record property (For example, the title of the book went after the author) I almost implemented it, but I got stuck at the moment when I would take the string to save the author / book title. Just write like I have A[d].Author:= A[d].Author + output[c] ? It just seems to me that this is somehow all through crutches, maybe I'm doing something wrong?
Record code:
TBook = record
Pages: integer;
Title: ShortString;
Author: ShortString;
end;

Load function code from file to records array
procedure TForm1.LoadBtn1Click(Sender: TObject);
var output: ShortString;
begin
assignfile(LoadF, 'LoadF.txt');
Reset(LoadF);
for d:=0 to Length(A) - 1 do
begin
Read(LoadF, output);
for c:=0 to Length(output) do
begin
if (output[c] = ' ') then count:= count + 1;
if (count = 1) then A[d].Author :=
end;
Read(LoadF, A[d].Author);
end;

closeFile(LoadF);
end;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2020-05-16
@Kezzya

procedure TMainForm.ButtonClick(Sender: TObject);
var
 men: TMatchCollectionEnumerator;
begin
  men := TRegEx.Matches('one two three', '\w+').GetEnumerator;

  while men.MoveNext do
  begin
    men.Current.Value
  end;

  men.Free;
end;

Got it?
Units
- System.RegularExpressions
- System.Generics.Collections

K
Konstantin Tsvetkov, 2020-05-16
@tsklab

how can I add records to an existing array, because I don’t know how many there will be
Use a dynamic array ( SetLength).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question