Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy this search can not generate correct result?
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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.</p> <p>The below is with Delphi 7.</p> <p>Thank you. </p> <pre><code>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 customer: string; begin //... Here is the code part that P1 is created as regular expression (omitted) while p1.MatchAgain do begin //p1 is regular expression customer:=p1.MatchedExpression; // try to match customer name. if (search(customer)=false) then begin //this line output wrong number // if (forloopsearch(customer)=false) then begin // this forloopsearch is ok list.Add(customer+'=1'); end; allcustomer:=allcustomer+1; // global allcustomer is integer to count all customers 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 &lt;= 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 &gt; 0 then r := m - 1 else l := m + 1; end; end; </code></pre> <p>Edit:</p> <p>Thank you all.</p> <p>In order to clarify the problem, I copy the test code from my computer here and I am sorry for my redundancy.</p> <p>Below is the name file that contains 3 persons' names (totally, 45 johns, 45 maries, and 45 erics).</p> <pre><code>mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, mary, mary, mary, john, john, john, eric, eric, eric, </code></pre> <p>The forloopsearch will generate the following. Below is a copy result that reads key and value and add into a listview (using add method of listview. items). This result is correct and is what I want.</p> <pre><code> mary 45 john 45 eric 45 </code></pre> <p>Below is forloopsearch.</p> <pre><code>Function Tform1.forloopsearch(customer: string): boolean; var fre:string; i: integer; aname:string; num:integer; begin result:=false; for i:=0 to list.count-1 do begin aname:=list.names[i]; if aname=customer then begin fre:=list.ValueFromIndex [i]; num:=strtoint(fre); num:=num+1; list.ValueFromIndex[i]:=inttostr(num); Result := true; End; end; end; </code></pre> <p>The forloopsearch function output above is correct and it modifies values of keys in iteration. </p> <p>The Search function will output the following. It is not correct and is not what I want. It also modifies values of keys in loop, but output is obviously wrong. </p> <p>In both cases, list.sorted is set true when form is created as shown above. They both use same procedure to iterate the list and copy key and value in order into a listview (by using add method). Below is Search function result.</p> <pre><code>mary 3 john 2 john 1 eric 1 eric 4 eric 40 mary 3 john 1 john 40 john 1 mary 39 </code></pre> <p>How to solve it. Thank you again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload