How to list foreach from array
-
wrote on 17 Mar 2012, 03:24 last edited by
Hi.
At this time I write second code:
@QStrinList list, list2;
...
list2.clear();
list2 << "data1" << "data2" ;
foreach (QString v, list2) list.removeOne(v);@
Is here a way to write simply without temporary list2 declaration?
@QStrinList list;
...
foreach (QString v, {"data1", "data2"}) list.removeOne(v);@
or
@QStrinList list;
...
QString array[] = {"data1", "data2"};
foreach (QString v, array) list.removeOne(v);@
In php, javascript and pascal it is possible. -
wrote on 17 Mar 2012, 12:11 last edited by
What exactly are you trying to a achieve?
As far as I know, foreach in Qt is completely the same as in php ( functionality, syntax is slightly different). -
wrote on 17 Mar 2012, 12:37 last edited by
Try it :
@ QStringList list;
list <<"One" <<"Two" <<"..." <<".....";
foreach (QString v,list){
v.replace("One","Ten");
qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
}@ -
wrote on 17 Mar 2012, 17:56 last edited by
If I get the question correctly, you want to remove a series of elements from a list efficiently. Right?
I'm not sure what syntactic sugar you want though. If you want to delete multiple items at once, you'll have to put those items in a list or array somehow anyway. Your second alternative is just as long as your original code, barring the needless list2.clear() call.
-
wrote on 18 Mar 2012, 08:30 last edited by
Try this:
@
QStrignList list;
...
foreach (QString v, QStringList({"data1", "data2"})) list.removeOne(v);
@This a 4.8 feature, C++11 only. QStringList::QStringList ( std::initializer_list<QString> args ) constructor is used.
-
wrote on 18 Mar 2012, 20:48 last edited by
[quote author="Andre" date="1332007005"]If I get the question correctly, you want to remove a series of elements from a list efficiently. Right?
I'm not sure what syntactic sugar you want though. If you want to delete multiple items at once, you'll have to put those items in a list or array somehow anyway. Your second alternative is just as long as your original code, barring the needless list2.clear() call. [/quote]
Remove a series of elements from the list is only one example of project.
Essentially I want to get array from database and then reflect output results to a program. Mainly using foreach function.
For example I have tables:
@0|data1
1|data2
2|data3
output:
array[] = ({"data1", "ident2", "ident2"});
foreach (Qstring v, array) {do something with v};or
1|"name1"|"data1"
2|"name2"|"data2"
5|"name3"|"data3"
output:
array[1] or array["name1"] = "data1"
array[2] or array["name2"] = "data2"
array[5] or array["name3"] = "data3"or
1|"name1"|"data11"|"data12"
2|"name2"|"data21"|"data22"
5|"name3"|"data31"|"data32"
output:
array[1] or array["name1"] = ({"data11", "data12"});
array[2] or array["name2"] = ({"data21", "data22"});
array[5] or array["name3"] = ({"data31", "data32"});
foreach (Qstring v, array["name2"]) {do something with v};or lang table:
id|name|en|lv
0|"name1"|"en text 1"|"lv text 1"
1|"name2"|"en text 2"|"lv text 2"
2|"name3"|"en text 3"|"lv text 3"
output:
array["en"] = {"en text 1", "en text 2", "en text 3"});
array["lv"] = {"lv text 1", "lv text 2", "lv text 3"});
and
array["en"][0] or array["en"]["name1"] = "en text 1"
array["en"][1] or array["en"]["name2"] = "en text 2"
array["en"][2] or array["en"]["name3"] = "en text 3"
array["lv"][0] or array["lv"]["name1"] = "lv text 1"
array["lv"][1] or array["lv"]["name2"] = "lv text 2"
array["lv"][2] or array["lv"]["name3"] = "lv text 3"
QString lang = "en";
foreach (Qstring v, array[lang]) {do something with v};
array[lang][1] or array[lang]["name2"] = "en text 2"
@
Every time make from array QList I think is not good solution.
[quote author="Kxyu" date="1332059432"]Try this:
@
QStrignList list;
...
foreach (QString v, QStringList({"data1", "data2"})) list.removeOne(v);
@
This a 4.8 feature, C++11 only. QStringList::QStringList ( std::initializer_list<QString> args ) constructor is used.[/quote]It would be perfect, but not working on latest "Offline installer - 1.3 GB/ Based on Qt 4.7.4(32bit)" on win 7. And where I can see C++ version? :)
May be I need to install Qt Creator and libraries separately? -
wrote on 19 Mar 2012, 11:27 last edited by
[quote author="francomartins" date="1331987833"]Try it :
@QStringList list;
list <<"One" <<"Two" <<"..." <<".....";
foreach (QString v,list) {
v.repleace("One","Ten");
qDebug()<<v ; //returns "Ten" , "Two" , "..." , "....."
@
[/quote]Sorry, but thats plain wrong for multiple reasons.
foreach creates a copy of the container you put into the second argument. So you are modifying the copy and not the original container. Second, for each QString in the container you create a copy with the first argument v. So you are not even modifying the copy of the container, but only copies of each item in it.
If you had actually run you snippet you would have seen that. BTW: It even doesn't compile, which proofs to me that you didn't run it. A slightly modified and running version is:
@
QStringList list;
list <<"One" <<"Two" <<"..." <<".....";qDebug() << "List before:" << list;
foreach (QString x, list) {
x.replace("One","Ten");
}qDebug() << "List after:" << list;
@and the output is - surprise:
@
List before: ("One", "Two", "...", ".....")
List after: ("One", "Two", "...", ".....")
@ -
wrote on 19 Mar 2012, 14:19 last edited by
Hello Mr Immortal, the mistake was not finishing the foreach. try it now
@ QStringList list,list2;
list <<"One" <<"Two" <<"..." <<".....";
foreach (QString v,list){
v.replace("One","Ten");
qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
};@
the qDebug () is in QString, QStringList, and not the more can be done also in the QStringList;
try it :
@
foreach (QString v,list){
v.replace("One","Ten");
list2 << v;
}
qDebug()<<list2.join(","); //returns "Ten" , "Two" , "..." , "....." @ -
wrote on 20 Mar 2012, 10:12 last edited by
How I tried "replace" works correctly. Only You must be careful. QStringList list<<"twenty one"<<"twenty two" with replace("one","ten") give result: "twenty ten","twenty two"
To francomartins. Maybe write just simple:
@QStringList list <<"One"<<"Two"<<"...";
list.replaceInStrings("One","Ten");
qDebug()<<list ; //returns ("Ten", "Two", "...")@
But is here somebody who can give an answer to a topic main question? -
wrote on 20 Mar 2012, 10:57 last edited by
@QStrinList list, list2;
...
list2.clear();
list2 << "data1" << "data2" ;
foreach (QString v, list2) list.removeOne(v);@
I replace with:
@QStrinList list;
...
foreach (v, QStringList()<<"data1"<<"data2") list.removeOne(v);@ -
wrote on 20 Mar 2012, 13:46 last edited by
Sure, it's less lines, but I doubt it is more readable. I find the readability issue important.
An interesting read on designing for readable code, and a good summary of why the Qt API looks the way it does, can be found in "The Little Manual of API Design":http://www4.in.tum.de/~blanchet/api-design.pdf . I think it's a good read.
1/11