Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TStringList and data values (algorithme) 1

Status
Not open for further replies.

michaenh

Programmer
Aug 7, 2002
205
NO
Hi

I have a TStringList called sFirm.

In the stringList I have for example these values:
sFirm[0]:='firm1';
sFirm[1]:='firm2';
sFirm[2]:='firm3';
sFirm[3]:='firm4';
sFirm[4]:='firm2';
sFirm[5]:='firm1';
sFirm[6]:='firm6';

I want to pull out the uniq firm once and put it on another stringList(or the same stringList, but the important is not to have two same firm as firm1 and frim2).

So the list I want to get is:
sTmp[0]:='firm1';
sTmp[1]:='firm2';
sTmp[2]:='firm3';
sTmp[3]:='firm4';
sTmp[4]:='firm6';


Thanks for your help.

Cheers
mha


 
This is fairly simple to do. One of the properties of TStringList is Duplicates. This can be set to dupIgnore in the new stringlist to avoid duplicates.

With your names for the stringlists your code should look something like this (assuming that you have created sTmp somewhere):
Code:
procedure CreateUniqueList;
var
 x: integer;
begin
 sTmp.Clear;
 sTmp.Duplicates := dupIgnore;
 for x := 0 to sFirm.Count - 1 do
  sTmp.Add ( sFirm[x] );
end;
I haven't tested this so there may be some typos.

Andrew
 
Hi

Interesting.. but I still get the same list...
sTmp and sFirm are the same..

Strange.. any ideas..

Many thanks for your reply..
Cheers,
mha
 
Hi again.

I sorted it out.. :)

sFirma.Clear;
sFirma.Sorted := True; //we forgot to sort the list.. :)
sFirma.Duplicates := dupIgnore;

Great towserbase!!!! Many thanks!!
A Star for you...

I would never thought:
>One of the properties of TStringList is Duplicates

Just that simple instead trying to make my own algorithme..

JIIPPPI!!
cheers,
mha
 
Apologies for missing the Sorted property. Well done in finding the answer and thanks for the star :)

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top