Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to list foreach from array
QtWS25 Last Chance

How to list foreach from array

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 40.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    betmens
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

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


      Code is poetry

      1 Reply Last reply
      0
      • F Offline
        F Offline
        francomartins
        wrote on last edited by
        #3

        Try it :

        @ QStringList list;

        list <<"One" <<"Two" <<"..." <<".....";

        foreach (QString v,list){
        v.replace("One","Ten");
        qDebug()<<v ; //returns "Ten" , "Two" , "..." , ".....";
        }@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kxyu
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              betmens
              wrote on last edited by
              #6

              [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?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [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", "...", ".....")
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  francomartins
                  wrote on last edited by
                  #8

                  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" , "..." , "....." @

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    betmens
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      betmens
                      wrote on last edited by
                      #10

                      @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);@

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        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 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved