Solved

TStringList - Get the item with no pair

Posted on 2010-09-22
10
497 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 263
automatically logging in via bde delphi 3 70
How to debug For loops? 3 46
Find and Replace Stream with 0s 8 57
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now