Hi, All:
Below is to find same customer and if he is in list, the number add one. If he is not in the list, just add him in the list.
I use Search function to do this, but failed and generated incorrect records. It can not find the customer or the right number of customers.
But if I use For..loop to iterate the list, it does well and can find the customer and add new customer in that for..loop search procedure. (I did not paste for ..loop search procedrue here).
Another problem is that there is no difference between setting list.sorted true and false. It seems Search function is not correct. This search function is from an example of delphi textbook.
The below is with Delphi 7.
Thank you.
Procedure Form1.create;
begin
list:=Tstringlist.create;
list.sorted:=true; // Search function will generate exactly Same and Incorrect
//records no matter list.sorted is set true or false.
list.duplicates:=dupignore;
..
end;
Procedure addcustomer;
var
..
begin
while p1.MatchAgain do begin //p1 is regular expression
customer:=p1.MatchedExpression;
if (search(customer)=false) then begin
list.Add(customer+'=1');
end;
allcustomer:=allcustomer+1;
..
end;
Function Tform1.search(customer: string): boolean;
var
fre:string;
num:integer;
L:integer;
R:integer;
M: Integer;
CompareResult: Integer;
found: boolean;
begin
result:=false;
found:=false;
L := 0;
R := List.Count - 1;
while (L <= R) and ( not found ) do
begin
M := (L + R) div 2;
CompareResult := Comparetext(list.Names[m]), customer);
if (compareresult=0) then
begin
fre:=list.ValueFromIndex [m];
num:=strtoint(fre);
num:=num+1;
list.ValueFromIndex[m]:=inttostr(num);
Found := True;
Result := true;
exit;
end
else if compareresult > 0 then
r := m - 1
else
l := m + 1;
end;
end;