Reputation: 25
I'm busy working on an old grade 11 question paper and my teacher didn't explain the significance of thing such as (ipos -1) can anyone explain why this needs to be done because at the moment i'm struggling to understand what ipos-1 and the copy and delete function does. code is provided below:
procedure TForm1.CreateBookCode1Click(Sender: TObject);
var icount,k,ipos:integer;
begin
richedit1.Clear;
richedit1.Lines.Add('Book Title'+#9+'Book Code');
for k:=1 to icount do
begin
ipos := pos(';',arrBooks[k]);
arrtitle[k] := copy(arrbooks[k],1,ipos-1);
delete(arrbooks[k],1,ipos);
ipos := pos(',',arrbooks[k]);
arrsurname[k]:= copy(arrbooks[k],1,ipos-1);
arrcode[k] := copy(arrsurname[k],1,3) +inttostr(k);
richedit1.Lines.add(arrtitle[k] + #9 + arrcode[k]);
end;
Upvotes: 2
Views: 134
Reputation: 14685
ipos := pos(';',arrBooks[k]);
arrtitle[k] := copy(arrbooks[k],1,ipos-1);
This says "set the k'th element of the arrtitle array to be the string of characters that are before the first semicolon in the k'th element of the arrbooks array.
In other words, the title of each element is the first part of the element, the bit before the semicolon.
The reason for subtracting 1 from ipos is that ipos is the position of the semicolon in the k'th element of arrbooks array. Subtracting one from the copy means that you don't copy the semicolon.
Note that there appears to be at least two problems here:
1) icount does not appear to be initialised. It should contain the number of elements in the arrbooks array.
2) In the line that sets ipos, arrbooks is mis-typed: it has a capital B
delete(arrbooks[k],1,ipos);
This says "remove all the characters from the k'th element of the arrbooks array from the beginning up to and including the semicolon (because ipos is pointing at the semicolon in the k'th elemet of the arrbooks array).
Upvotes: 4