Board index » cppbuilder » How is it possible to delete "REAL" identical strings out of a StringList..?

How is it possible to delete "REAL" identical strings out of a StringList..?


2004-10-14 09:12:45 AM
cppbuilder55
dear builders,
how is it possible to delete "REAL" identical strings out of a StringList..?
i've used the IndexOf( ) but i've missed that it's case-sensitive..thats why
the following will fail for these lines:
----------------------------------------
a
A
b
B
----------------------------------------
Result
----------------------------------------
a
b
----------------------------------------
/****************************************************/
void __fastcall TfrmMultipleFile::Kill_Identical_Lines()
{
TStringList *sList = new TStringList;
try
{
int x = 0;
while(x < frmDeleteLines->re->Lines->Count)
{
if(sList->IndexOf(frmDeleteLines->re->Lines->Strings[x]) < 0)
sList->Add(frmDeleteLines->re->Lines->Strings[x]); x++;
};
re->Lines->Clear(); Lines->AddStrings(sList); sList->Clear();
}
__finally {delete sList;}
}
void __fastcall TfrmMultipleFile::cmdAddFilesClick(TObject *Sender)
{
if(OpenDialog->Execute())
{
ListBox1->Items->AddStrings(OpenDialog->Files);
// Kill_Identical_Lines();
// Kill_DoubleItems(FileBox->Items);
}
}
// this one also want work..
void __fastcall TfrmMultipleFile::Kill_DoubleItems(TStrings* List)
{
if(List->Count>0)
{
for(int i = 0; i < List->Count; i++)
{
for(int k = i; k < List->Count; k++)
{
if(List->Strings[i] == List->Strings[k]) List->Delete(k);
}
}
}
}
/****************************************************/
any ideas..?
Oren
 
 

Re:How is it possible to delete "REAL" identical strings out of a StringList..?

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
i've used the IndexOf( ) but i've missed that it's case-sensitive..
You will just have to loop manually comparing the strings case-insensitively
yourself. This code:
int x = 0;
while(x < frmDeleteLines->re->Lines->Count)
{
if(sList->IndexOf(frmDeleteLines->re->Lines->Strings[x]) < 0)
sList->Add(frmDeleteLines->re->Lines->Strings[x]); x++;
};
Becomes this instead:
for(int x = 0; x < frmDeleteLines->re->Lines->Count; ++x)
{
AnsiString line = frmDeleteLines->re->Lines->Strings[x];
bool found = false;
for(int y = 0; (y < sList->Count) && (!found); ++y) {
found = (sList->Strings[y].AnsiCompareIC(line) == 0);
}
if( !found )
sList->Add(line);
};
Quote
if(List->Strings[i] == List->Strings[k])
That is a case-sensitive comparison.
Gambit
 

Re:How is it possible to delete "REAL" identical strings out of a StringList..?

Quote
Becomes this instead:

for(int x = 0; x < frmDeleteLines->re->Lines->Count; ++x)
{
AnsiString line = frmDeleteLines->re->Lines->Strings[x];
bool found = false;

for(int y = 0; (y < sList->Count) && (!found); ++y) {
found = (sList->Strings[y].AnsiCompareIC(line) == 0);
}

if( !found )
sList->Add(line);
};


>if(List->Strings[i] == List->Strings[k])

Gambit
Remy, look...it still deletes wrong...
----------------------------------------
a
A
b
B
----------------------------------------
the result i get with your code is:
----------------------------------------
a
b
----------------------------------------
but it's wrong because NOTHING from
above is identical, so result should stay:
----------------------------------------
a
A
b
B
----------------------------------------
what is wrong...?
/*************************************************/
try
{
int count = frmDeleteLines->re->Lines->Count;
for(int x = 0; x < count; ++x)
{
AnsiString line = frmDeleteLines->re->Lines->Strings[x];
bool found = false;
for(int y = 0; (y < sList->Count) && (!found); ++y)
{
found = (sList->Strings[y].AnsiCompareIC(line) == 0);
}
if(!found) sList->Add(line);
};
frmDeleteLines->re->Lines->Clear();
frmDeleteLines->re->Lines->AddStrings(sList);
sList->Clear();
}
/*************************************************/
Oren
 

{smallsort}

Re:How is it possible to delete "REAL" identical strings out of a StringList..?

Use AnsiCompareIC if you are doing a case-insensitive compare. If you
care about case sensitivity, use AnsiCompare instead.
--
Leo Saguisag
Delphi l10n engineer
About the Borland newsgroups: info.borland.com/newsgroups/
 

Re:How is it possible to delete "REAL" identical strings out of a StringList..?

"Leo Saguisag (Borland)" <lsaguisag_at_borland_dot_com>schrieb
im Newsbeitrag news:416ddd38$ XXXX@XXXXX.COM ...
Quote
Use AnsiCompareIC if you are doing a case-insensitive compare. If you
care about case sensitivity, use AnsiCompare instead.

Leo Saguisag
Ohhhh.....didn't see that ! Thanks Leo :-)
Oren
 

Re:How is it possible to delete "REAL" identical strings out of a StringList..?

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
the result i get with your code is:
As well you should, because that is what you asked for - case-insensitive
searching.
Quote
but it's wrong because NOTHING from
above is identical, so result should stay:
Your original code already produces that exact result. There is no way
IndexOf() will decide that "a" and "A" are identical, thus there is no way
your original code could have been filtering those values out to begin with.
They both should have ended up in the result just fine.
Gambit