N
N
nalimite2017-02-14 09:05:06
Delphi
nalimite, 2017-02-14 09:05:06

Why doesn't the tStringList comparison work?

Hello.
I have two tStringList (MainBase, Domain)
The first contains mail, the second contains domains respectively.
I need to check each line in MainBase whether each domain from the list is present in the line, if yes, then add it to another stringlist, if not, to another stringlist.
Here is my code :

for i:=0 to MainBase.Count-1   do
   begin
    for j := 0 to Domains.Count-1 do
      begin
        if Pos(Domains[j], MainBase[i])>0 then
          begin
            NormalBase.Add(MainBase[i]);
          end else begin
            nNormalBase.Add(MainBase[i]);
          end;
      end;
   end;

The problem is that with such work, if a domain is found, it adds to the third sheet (NormalBase), and in the second (nNormalBase) absolutely all lines are added, regardless of whether a match is found or not.
Also in nNormalBase, when adding, everything is duplicated as many times as there are domains in the stringlist.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2017-02-14
@OLS

Actually, that's how you wrote the code. Since for each domain there is a case of NOT matching with any of the mail addresses, then each will fall into the "bad" list, and more than once. If you need to find a list of domains that does not correspond to NONE of the listed email addresses, then it is more logical, after compiling the "good" list, to calculate the "bad" one in the second pass by subtracting the set of "good" domains from the set of all domains.

K
kalapanga, 2017-02-14
@kalapanga

Get a Boolean flag Found. Drop it before looping over Domains. In the loop, when the condition under test is met, add a line to NormalBase, check the flag and break the loop (break) - why twist further? After looping through Domains, check the box. If not set, there were no matches - add a line to nNormalBase.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question