Results 1 to 11 of 11

Thread: QByteArray.split() returning QList with empty first element

  1. #1
    Join Date
    Jun 2013
    Posts
    20
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default QByteArray.split() returning QList with empty first element

    Hey Guys,

    I got a QByteArray containing some data. I distinguish different kinds of data via key characters which I prepend with a '&' character. Currently the data QByteArray only contians something like this: data = "&t02.04.2014".

    Qt Code:
    1. void foo(QByteArray data){
    2.  
    3. QList<QByteArray> cmdList = data.split('&');
    4.  
    5. if(cmdList.size() != data.count('&')) dispError("Command List size != number of '&'");
    6. //At this point the cmdList contains 2 elments where the first one is empty (presuming the data array is like noted above.
    7.  
    8. //do fancy stuff with the data
    9. }
    To copy to clipboard, switch view to plain text mode 

    I don't quite understand why there are two elments in the QList as I am only expecting one.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray.split() returning QList with empty first element

    As a special character is treated as a marker for the end of the element. String "&&&aaa" gives You 4 elements.

  3. The following user says thank you to Lesiok for this useful post:

    FreddyKay (4th February 2015)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray.split() returning QList with empty first element

    In other words "&" separates items. Therefore by definition the number of items in the array equals to number of separators + 1. Since you have one occurence of the separator, you have two items (no separators would yield one item -- the whole input string).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    FreddyKay (4th February 2015)

  6. #4
    Join Date
    Jun 2013
    Posts
    20
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QByteArray.split() returning QList with empty first element

    right. Gotcha. Thank you!

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QByteArray.split() returning QList with empty first element

    Take a look at the other split() arguments if you want to suppress empty sections.

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray.split() returning QList with empty first element

    Quote Originally Posted by ChrisW67 View Post
    Take a look at the other split() arguments if you want to suppress empty sections.
    QByteArray::split don't have other arguments, QString::split has them.

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray.split() returning QList with empty first element

    Having read the thread again I think the OP should use regular expressions rather than split.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Jun 2013
    Posts
    20
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QByteArray.split() returning QList with empty first element

    Quote Originally Posted by wysota View Post
    Having read the thread again I think the OP should use regular expressions rather than split.
    And why is that?

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray.split() returning QList with empty first element

    Because you want to "distinguish" things rather than split columns. At least that's what I understood.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Jun 2013
    Posts
    20
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QByteArray.split() returning QList with empty first element

    Quote Originally Posted by wysota View Post
    Because you want to "distinguish" things rather than split columns. At least that's what I understood.
    What I ultimatelly will have will be Array like this: [&t25.01.2015&satFancy Name//6000//Blablubb//something//42&antRunning//0//Illuminati&gs6300//2655].

    Each of the & indicates that the following stuff represents some values for objects that are specified by th name that follows '&'. The '//' seperate different values. Would it still be possible to use Regular Expressions? From first glance I figured split was what I wanted but you can teach me better

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray.split() returning QList with empty first element

    Yes, you can still use regular expressions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 9
    Last Post: 23rd April 2012, 14:53
  2. QByteArray split out data
    By jeffmetal in forum Qt Programming
    Replies: 9
    Last Post: 28th May 2011, 08:24
  3. QByteArray split to a list
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 11:49
  4. split QByteArray
    By xproch13 in forum Newbie
    Replies: 2
    Last Post: 29th October 2010, 22:50
  5. Replies: 4
    Last Post: 20th August 2010, 14:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.