Solved

TStringList - Get the item with no pair

Posted on 2010-09-22
10
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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