Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Test if string is within a string list

Posted on 2004-11-21
4
Medium Priority
?
208 Views
Last Modified: 2010-04-05
Hi all.

I have 2 different string lists and I am wanting to test if all strings in one list are in the other one and if not, add the string to another list for results.
I am using the following routine:
//-------------------------------------------------------------
procedure TForm1.btnCheckClick(Sender: TObject);
var
i: Integer;
sStr: String;
begin
lbResults.Clear;
  for i := 0 to lbCheck.Items.Count -1 do
  begin
  Application.ProcessMessages;
  sStr := lbCheck.Items.Strings[i];
    if lbAgainst.Items.IndexOf(sStr) = -1 then
    begin
    lbResults.Items.Add(lbCheck.Items[i]);
    end;
  end;
MessageDlg('Checking Complete',mtInformation,[MBOK],0);
end;

//-----------------------------------------------------------------------------------------

It seems that the routine above is not working at all and is simply giving me the full list of items in the lbCheck list box.

Is there something simple I am missing here or even better, is there a better way to do this??

Cheers.
Colin..
0
Comment
Question by:BedouinDN
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 300 total points
ID: 12640548
I tried your code and it seems to work fine.

Everything that was in list lbCheck and not in lbAgainst was copied to lbResult.

This also works:

begin
  lbResults.Clear;
  for i := 0 to lbCheck.Items.Count -1 do begin
    sStr := lbCheck.Items[i];
    if lbAgainst.Items.IndexOf(sStr) = -1 then begin
      lbResults.Items.Add(sStr); //use sStr, since it already has the value
    end;
  end;
end;
0
 
LVL 4

Author Comment

by:BedouinDN
ID: 12640563
It did work with only a few strings in either list, however I now have about 7500 items in each list and the code is not handling it for some reason. I am getting a full list of all items in the lbCheck list box even though I know that there are duplicates in the lists.
:-(
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12640666
I tried it with lists of 8000 in lbCheck and lbAgainst. It still seems to work. I verified it by displaying the number of items in each list after running the check. The result list showed 4000 items.

You can probably get some speed by keeping the list sorted (Sorted:=True) and using a binary search. I wrote a simple one in 'find'. It seems to speed it up, although I didn't do any official timing.

function Find(aStr:String; aList:tStrings):Integer;
var
  C,H,L,R:integer;
  Fnd:boolean;
begin
  { Assumes then aList is sorted }
  C:=aList.Count;
  H:=C-1;
  L:=0;
  repeat
    R:=((H-L) div 2)+L;
    Fnd:=(aList[R]=aStr);
    if not Fnd then begin
      if aStr>aList[R] then
        L:=R+1
      else
        H:=R-1;
    end;
  until Fnd or (L>H);
  if Fnd then
    Result:=R
  else
    Result:=-1;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  i:Integer;
  sStr: String;
begin
  lbResults.Clear;
  for i := 0 to lbCheck.Items.Count -1 do begin
    sStr := lbCheck.Items[i];
    if
     {lbAgainst.Items.IndexOf(sStr) = -1}
     Find(sStr,lbAgainst.Items)=-1  //faster!
    then begin
      lbResults.Items.Add(sStr);
    end;
  end;
  label3.caption:=inttostr(lbResults.Items.Count);
end;
0
 
LVL 4

Author Comment

by:BedouinDN
ID: 12640690
Thanks for your help, it seems Monday morning has been too much for me.. :-(
Turns out there was trailing whitespace after the strings in one of the lists, so I have added a TRIM function to get rid of them and now the code works..
Thanks again for your time..

Colin.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question