Solved

TStringList - Get the item with no pair

Posted on 2010-09-22
10
500 Views
Last Modified: 2012-06-27
I have a 2 TStringList;

TSTringList-1 with;
john
nick
jason
rose
ryan
xen

TSTringList-2 with;
john
nick
jason
ryan
xen

How could I get the item("rose") with no pair in both TStringList-1&2
var
nams,nums:TStringList;
i,e:integer;
begin
Nams := TStringList.Create;
Nams.add('john');
Nams.add('nick');
Nams.add('jason');
Nams.add('rose');
Nams.add('ryan');
Nams.add('xen');

Nums := TStringList.Create;
Nums.add('john');
Nums.add('nick');
Nums.add('jason');
Nums.add('ryan');
Nums.add('xen');


for i := 0 to Nams.Count - 1 do
begin
for e:= 0 to nums.Count -1 do
begin
//get "rose" or items with no pair value
//dont know here, my codes are not better
//if e<>i then Listbox3.Items.Add(?);
end;
end;

end;

Open in new window

0
Comment
Question by:systan
  • 5
  • 3
  • 2
10 Comments
 
LVL 19

Expert Comment

by:Barthax
ID: 33734628
To exit the for loops with the first, you could something like (warning: my Delphi is rusty):

var
found : Boolean;
orphan: string;
[[... etc.]]]

for i := 0 to Nams.Count - 1 do
begin
found := false;

for e:= 0 to nums.Count -1 do
begin
  if Nams[i] == nums[i] then
  begin
    found := true;
    break;
  end;
end;

if not found then
begin
return Nams[i];
end;
end;

0
 
LVL 14

Author Comment

by:systan
ID: 33734791
The psuedo code is not working.
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 250 total points
ID: 33734987
i don't it will be very fast using a TStringList


procedure TForm1.Button1Click(Sender: TObject);
var
  nams,nums, sx:TStringList;
  i,e:integer;
  temp: string;
begin
  Nams := TStringList.Create;
  try
    Nams.add('john');
    Nams.add('nick');
    Nams.add('jason');
    Nams.add('rose');
    Nams.add('ryan');
    Nams.add('xen');

    Nums := TStringList.Create;
    try
      Nums.add('john');
      Nums.add('nick');
      Nums.add('jason');
      Nums.add('ryan');
      Nums.add('xen');

      sx := TStringList.Create;
      try
        for i := 0 to Nams.Count - 1 do
        begin
          if Nums.IndexOf(Nams[I]) < 0 then
            sx.Add(Nams[I] + ' does not exist in Nums');
        end;
        for i := 0 to Nums.Count - 1 do
        begin
          if Nams.IndexOf(Nums[I]) < 0 then
            sx.Add(Nums[I] + ' does not exist in Nams');
        end;
        Memo1.Lines.AddStrings(sx);
      finally
        sx.Free;
      end;
    finally
      Nums.Free;
    end;
  finally
    Nams.Free;
  end;
end;

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33735029
>>Barthax ... you forgot to switch from PHP to Delphi
Nams[i] == nums[i] // PHP coding

Nams[i] = nums[i] // Delphi coding

Welcome back to the delphi world of coding ;-)


0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33735045
i forgot a word in my earlier comment
it was something that i try to avoid actually : the word "think"

>>i don't think it will be very fast using a TStringList
0
 
LVL 14

Author Closing Comment

by:systan
ID: 33735066
Geert?
Your rank reflects your skills.

Thanks
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33735123
skills ?
I just upped my skills with some good comments i found :

// drunk, fix later

// at the time I wrote this, only god and I knew what the code was doing
// now only god knows

// repeat
//   church code ...
// until (JezusChristReturns);

// You got this far reading my code, and you still understand it ?
// The rest gets worse, get your aspirin ready !
// May the source be with you
0
 
LVL 14

Author Comment

by:systan
ID: 33735155
Oh, really forgot this kind of code; it's so simple.

       sx := TStringList.Create;
        for i := 0 to Nams.Count - 1 do
        begin
          if Nums.IndexOf(Nams[I]) < 0 then
            sx.Add(Nams[I]);
        end;


forgetfull ;_
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33735188
on the oracle database:

select * from Nums
minus
select * from Nams

or vice versa

but this is delphi ... uguhuhuhu
0
 
LVL 19

Expert Comment

by:Barthax
ID: 33742503
Thanks to Geert for recognising my rustiness. ;)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 105
Working with hours 3 65
Find and Replace Stream with 0s 8 69
how to update exe applicatio from internet ? 6 87
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

829 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