March 17, 2012

betmens betmens
Lab Rat
18 posts

How to list foreach from array

 

Hi.
At this time I write second code:

  1. QStrinList list, list2;
  2. ...
  3. list2.clear();
  4. list2 << "data1" << "data2" ;
  5. foreach (QString v, list2) list.removeOne(v);

Is here a way to write simply without temporary list2 declaration?
  1. QStrinList list;
  2. ...
  3. foreach (QString v, {"data1", "data2"}) list.removeOne(v);

or
  1. QStrinList list;
  2. ...
  3. QString array[] = {"data1", "data2"};
  4. foreach (QString v, array) list.removeOne(v);

In php, javascript and pascal it is possible.

10 replies

March 17, 2012

Jake007 Jake007
Robot Herder
248 posts

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).

 Signature 

——————————————-
Code is poetry

March 17, 2012

francomartins francomartins
Hobby Entomologist
61 posts

Try it :

  1.   QStringList  list;
  2.  
  3.    list <<"One" <<"Two" <<"..." <<".....";
  4.  
  5.    foreach (QString v,list){
  6.     v.replace("One","Ten");
  7.    qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
  8.    }

March 17, 2012

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 18, 2012

Kxyu Kxyu
Lab Rat
145 posts

Try this:

  1. QStrignList list;
  2. ...
  3. 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.

March 18, 2012

betmens betmens
Lab Rat
18 posts

Andre wrote:
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.

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:

  1. 0|data1
  2. 1|data2
  3. 2|data3
  4. output:
  5. array[] = ({"data1", "ident2", "ident2"});
  6. foreach (Qstring v, array) {do something with v};
  7.  
  8. or
  9. 1|"name1"|"data1"
  10. 2|"name2"|"data2"
  11. 5|"name3"|"data3"
  12. output:
  13. array[1] or array["name1"] = "data1"
  14. array[2] or array["name2"] = "data2"
  15. array[5] or array["name3"] = "data3"
  16.  
  17. or
  18. 1|"name1"|"data11"|"data12"
  19. 2|"name2"|"data21"|"data22"
  20. 5|"name3"|"data31"|"data32"
  21. output:
  22. array[1] or array["name1"] = ({"data11", "data12"});
  23. array[2] or array["name2"] = ({"data21", "data22"});
  24. array[5] or array["name3"] = ({"data31", "data32"});
  25. foreach (Qstring v, array["name2"]) {do something with v};
  26.  
  27. or lang table:
  28. id|name|en|lv
  29. 0|"name1"|"en text 1"|"lv text 1"
  30. 1|"name2"|"en text 2"|"lv text 2"
  31. 2|"name3"|"en text 3"|"lv text 3"
  32. output:
  33. array["en"] = {"en text 1", "en text 2", "en text 3"});
  34. array["lv"] = {"lv text 1", "lv text 2", "lv text 3"});
  35. and
  36. array["en"][0] or array["en"]["name1"] = "en text 1"
  37. array["en"][1] or array["en"]["name2"] = "en text 2"
  38. array["en"][2] or array["en"]["name3"] = "en text 3"
  39. array["lv"][0] or array["lv"]["name1"] = "lv text 1"
  40. array["lv"][1] or array["lv"]["name2"] = "lv text 2"
  41. array["lv"][2] or array["lv"]["name3"] = "lv text 3"
  42. QString lang = "en";
  43. foreach (Qstring v, array[lang]) {do something with v};
  44. array[lang][1] or array[lang]["name2"] = "en text 2"

Every time make from array QList I think is not good solution.
Kxyu wrote:
Try this:
  1. QStrignList list;
  2. ...
  3. 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.

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?

March 19, 2012

Volker Volker
Robot Herder
5428 posts

francomartins wrote:
Try it :

  1. QStringList  list;
  2.  
  3. list <<"One" <<"Two" <<"..." <<".....";
  4.  
  5. foreach (QString v,list) {
  6.  v.repleace("One","Ten");
  7. qDebug()<<v ; //returns "Ten" , "Two" , "..." , "....."

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:

  1. QStringList  list;
  2. list <<"One" <<"Two" <<"..." <<".....";
  3.  
  4. qDebug() << "List before:" << list;
  5.  
  6. foreach (QString x, list) {
  7.     x.replace("One","Ten");
  8. }
  9.  
  10. qDebug() << "List after:" << list;

and the output is – surprise:

  1. List before: ("One", "Two", "...", ".....")
  2. List after: ("One", "Two", "...", ".....")

March 19, 2012

francomartins francomartins
Hobby Entomologist
61 posts

Hello Mr Immortal, the mistake was not finishing the foreach. try it now

  1.    QStringList  list,list2;
  2.  
  3.    list <<"One" <<"Two" <<"..." <<".....";
  4.  
  5.    foreach (QString v,list){
  6.     v.replace("One","Ten");
  7.    qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
  8.    };

the qDebug () is in QString, QStringList, and not the more can be done also in the QStringList;
try it :
  1.    foreach (QString v,list){
  2.     v.replace("One","Ten");
  3.    list2 << v;
  4.    }
  5. qDebug()<<list2.join(","); //returns "Ten" , "Two" , "..." , "....."

March 20, 2012

betmens betmens
Lab Rat
18 posts

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:

  1. QStringList list <<"One"<<"Two"<<"...";
  2. list.replaceInStrings("One","Ten");
  3. qDebug()<<list ; //returns ("Ten", "Two", "...")

But is here somebody who can give an answer to a topic main question?

March 20, 2012

betmens betmens
Lab Rat
18 posts

  1. QStrinList list, list2;
  2. ...
  3. list2.clear();
  4. list2 << "data1" << "data2" ;
  5. foreach (QString v, list2) list.removeOne(v);

I replace with:
  1. QStrinList list;
  2. ...
  3. foreach (v, QStringList()<<"data1"<<"data2") list.removeOne(v);

March 20, 2012

Andre Andre
Area 51 Engineer
6031 posts

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 [.in.tum.de] . I think it’s a good read.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ QThread, signal/slots      Subclassing QGraphicsScene ››

You must log in to post a reply. Not a member yet? Register here!