0

I want to know how can I compare 2 QStringList and display the results in the following way:

I have 2 QPlainTextEdit A and B

  • In A I have, for example, 10 lines of 6 numbers per line;
  • In B i have, for example, 100 lines of 6 numbers per line;

What I want to do is this: take each line in B and compare with all the lines in A. If, in one line-line comparison, all 6 are equal to int n6 (for example) will add 1. If only 5 are equal, the int n5 will add 1 and so on.

I've tried a few things but none of them worked. If you could give some light on this I'll appreciate.

msrd0
  • 7,816
  • 9
  • 47
  • 82
Rafaelpsmed
  • 67
  • 2
  • 10

2 Answers2

2

Use QSet and subtract() and count() or size()

QStringList mOldList, mNewList;

  mOldList.append("1");
  mOldList.append("2");
  mOldList.append("3");
  mOldList.append("4");
  mOldList.append("5");
  mOldList.append("10");

  mNewList.append("1");
  mNewList.append("2");
  mNewList.append("3");
  mNewList.append("4");
  mNewList.append("5");
  mNewList.append("15");

QSet<QString> subtraction = mNewList.toSet().subtract(mOldList.toSet());
QSet<QString> subtraction1 = mOldList.toSet().subtract(mNewList.toSet());
foreach (const QString &filename, subtraction)
        qDebug() << " difference: "<<  filename;
foreach (const QString &filename, subtraction1)
        qDebug() << " difference: "<<  filename;

Result:

 difference:  "15" 
 difference:  "10" 

For example with next lists:

  mOldList.append("1");
  mOldList.append("2");
  mOldList.append("3");
  mOldList.append("4");
  mOldList.append("5");
  mOldList.append("10");

  mNewList.append("1");
  mNewList.append("2");
  mNewList.append("3");
  mNewList.append("4");
  mNewList.append("5");
  mNewList.append("15");

QSet<QString> subtraction = mNewList.toSet().subtract(mOldList.toSet());
foreach (const QString &fileName, subtraction)
        qDebug() << " difference: "<<  fileName;

Result only 15:

 difference:  "15" 

It means that one element is not same.

  mOldList.append("1");
  mOldList.append("2");
  mOldList.append("3");
  mOldList.append("4");
  mOldList.append("5");
  mOldList.append("10");

  mNewList.append("1");
  mNewList.append("2");
  mNewList.append("3");
  mNewList.append("4");
  mNewList.append("115");
  mNewList.append("15");

Two elements are not same:

 difference:  "115" 
 difference:  "15"

Or another way:

qSort(mNewList);
qSort(mOldList);

if(mNewList == mOldList){
    qDebug() << "same";
}
else{
    qDebug() << "not same";
}
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • thanks for your answer but how i count this difference? – Rafaelpsmed Oct 11 '14 at 21:38
  • @Rafaelpsmed your difference there is in QSet, so just call count() method for substraction. It returns you number of elements in QSet, but it is a number which you are looking for. – Jablonski Oct 12 '14 at 04:51
  • thanks it worked. but how i compare the first line in QPlainText B with all the lines in QPlainText A? and then compare the second line in B with all the lines in A and so on? – Rafaelpsmed Oct 14 '14 at 01:31
1

Start by calculating the number of matching digits per string

int matchingDigits(QString str1, QString str2)
{
    int matches = 0;
    int minSize = str1.size() < str2.size() ? str1.size() : srt2.size();
    for (int pos = 0; pos < minSize; ++pos)
    {
        if (str1.at(pos) == str2.at(pos) ++matches;
    }
    return matches;
}

Now you compare all a strings with each other (iterating over both StringLists) and if matchingDigits() is something > 0 then you can increase your result counters.

QStringList listA;
QStringList listB;

foreach (QString a, listA)
{
    foreach (QString b, listB)
    {
        int matches = matchingDigits(a, b);
        if (matches > 0)
        {
            // do something fancy
        } 
    }
}
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • thanks for your answer but I forgot to say that this will be a lottery check app. Your method works but it returns 11 when 1;2;3;4;5;6 is against 1;2;3;4;5;6 (because of the ; I guess) and returns 3 when 1;2;3;4;5;6 against 23;53;46;60;13;24. How solve this? – Rafaelpsmed Oct 11 '14 at 21:23
  • Nevermind i got it. So how I can compare all strings with each other? – Rafaelpsmed Oct 11 '14 at 21:47
  • @Rafaelpsmed, I edited my answer to add a basic construction of the comparison. But is you have numbers > 9, the string comparison is not the best idea. You should first create a `QStringList` for each line, using http://qt-project.org/doc/qt-5/qstring.html#split – Simon Warta Oct 12 '14 at 08:06