Solved

TStringList - Get the item with no pair

Posted on 2010-09-22
10
495 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
Comment Utility
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
Comment Utility
The psuedo code is not working.
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 250 total points
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
>>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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 14

Author Closing Comment

by:systan
Comment Utility
Geert?
Your rank reflects your skills.

Thanks
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
Thanks to Geert for recognising my rustiness. ;)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

8 Experts available now in Live!

Get 1:1 Help Now