PDA

View Full Version : QStringList ......how to ignore a string



:db:sStrong
7th August 2006, 11:08
Hi folks,

I have a question on QstringLists.. I have a list of strings which is produced and in that list, i have a string which can have the same name like..

john_1
john_2
john_1
john_2

How can i ignore the last two strings and show only the first two ones??

here is my code and explanation...


//this is the list which will see how many category of the names are there
// for example two category :
//1- NameList One
//2- NameList Two

QStringList test1 = project->getTest1List();

for ( unsigned int i=0; i <test1.size(); i++ )
{

// this is the list which will look inside the above two categories and collect
//the names for example :

//NameList One: has the following names
//john_1
//john_2

//NameList Two: has the following names
//john_1
//john_2
//john_3

//I get now the following:
//john_1
//john_2
//john_1
//john_2
//john_3

// i want the following results:
//john_1
//john_2
//john_3

QStringList totall = test->getfromFunction();
SomethingItem * someItem;

someItem->getItem()->setText(NAME, QString("test: %1").arg(totall.size()));
someItem->getItem()->setOpen(false);

for ( unsigned int j=0; j <totall.size(); j++ )
{

someItem->setName(totall[j]);

}
}

sunil.thaha
7th August 2006, 11:17
Before inserting into the list, can't you check if the string exists :confused:

Kumosan
7th August 2006, 11:35
Use your strings as keys for a QMap. After that you can get a QList with unique string from QMap::keys().

Easy and quick hack if performance is not that of a problem.

:db:sStrong
7th August 2006, 12:07
Before inserting into the list, can't you check if the string exists :confused:

no because the list is produced by database and based on situation i get the categories of name.. it can be 10 categories and each can have the same names.. i just want to ignore the names which are same..

Kumosan
7th August 2006, 12:16
Just seen: QStringList takes a QList<QString> parameter.

So QMap<QString bla, bool whocares> bla;
bla.insert("string1",0);
bla.insert("string2",0);

QStringList(bla.keys()) <- Your stringlist with unique strings.

Don't take my 'example code' too literal. ;-)

:db:sStrong
7th August 2006, 12:47
Just seen: QStringList takes a QList<QString> parameter.

So QMap<QString bla, bool whocares> bla;
bla.insert("string1",0);
bla.insert("string2",0);

QStringList(bla.keys()) <- Your stringlist with unique strings.

Don't take my 'example code' too literal. ;-)

no it cant work on this way.. because i never know which strings are the same, every time i get different strings.. i think i should make another list and insert the strings which will come one time and check if the strings are the same should not be inserted.. but how to implement dont know :(

Kumosan
7th August 2006, 12:55
no it cant work on this way.. because i never know which strings are the same, every time i get different strings.. i think i should make another list and insert the strings which will come one time and check if the strings are the same should not be inserted.. but how to implement dont know :(

I don't quite understand. You don't need to know which strings are the same. The keys of a QMap are automaticall unique.

You do twenty times:

bla["someString"] = true;

forty times:

bla["anotherString"] = true;

one time:

bla["lastString"] = true;

and the QList<String> bla.keys() contains exactly three strings:

"someString" and "anotherString" and "lastString".

If you initialize your QStringList with it you have your stringlist with unique strings. You don't need to know which strings are the same. QMap does this automatically.

sunil.thaha
8th August 2006, 05:45
Since it is database U can either use distinct or group by
to eliminate the repeated !!!

sumsin
8th August 2006, 06:19
make a sub-function



bool isNameExistInList(QStringList &list, QString &name) {
for(QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
if(name == *it) {
return TRUE;
}
}
return FALSE;
}


then use this function at the time of insertion of strings



if(!isNameExistInList(yourList, "john_1") {
yourList.append("john_1")
}

and so on...

:db:sStrong
8th August 2006, 10:48
make a sub-function



bool isNameExistInList(QStringList &list, QString &name) {
for(QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
if(name == *it) {
return TRUE;
}
}
return FALSE;
}


then use this function at the time of insertion of strings



if(!isNameExistInList(yourList, "john_1") {
yourList.append("john_1")
}

and so on...


hello,
thanx this is working for me but the Qmap is not needed in this situation ..

sunil.thaha
8th August 2006, 15:04
or



if( ! list.contains( name ) ) {
list << name;
}