PDA

View Full Version : Compare QString alphabet position



patrik08
2nd July 2006, 09:30
compare qstring...

I search a similar QString function from php strcmp
http://php.net/strcmp to find the nummer on 2° position from a qmap...

How make this...

Example ... if your name is "zumalli" the formula take only the first 3 lovercase letter
"zum" and i musst return 993

if Your name is "zcello" take zce and from compare alphabet position i must return 979




typedef QMap<int, QStringList> TableAi;
TableAi Box_att;
Box_att.clear();
................................................
Box_att.insert(875,QStringList() << "x" << "975");
Box_att.insert(876,QStringList() << "z" << "976");
Box_att.insert(877,QStringList() << "zam" << "977");
Box_att.insert(878,QStringList() << "zau" << "978");
Box_att.insert(879,QStringList() << "zb" << "979");
Box_att.insert(880,QStringList() << "ze" << "980");
Box_att.insert(881,QStringList() << "zeh" << "981");
Box_att.insert(882,QStringList() << "zei" << "982");
Box_att.insert(883,QStringList() << "zem" << "983");
Box_att.insert(884,QStringList() << "zf" << "984");
Box_att.insert(885,QStringList() << "zi" << "985");
Box_att.insert(886,QStringList() << "zim" << "986");
Box_att.insert(887,QStringList() << "zin" << "987");
Box_att.insert(888,QStringList() << "zk" << "988");
Box_att.insert(889,QStringList() << "zo" << "989");
Box_att.insert(890,QStringList() << "zu" << "990");
Box_att.insert(891,QStringList() << "zuc" << "991");
Box_att.insert(892,QStringList() << "zul" << "992");
Box_att.insert(893,QStringList() << "zum" << "993");
Box_att.insert(894,QStringList() << "zun" << "994");
Box_att.insert(895,QStringList() << "zur" << "995");
Box_att.insert(896,QStringList() << "zus" << "996");
Box_att.insert(897,QStringList() << "zw" << "997");
Box_att.insert(898,QStringList() << "zwe" << "998");
Box_att.insert(899,QStringList() << "zy" << "999");


/* incomming form value nam */
QString firsttree="000";
QString nametree="zce"; /* must become alphabet 979 */

TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it ) {
QStringList itemsetter = it.value();
QString letter = QString(itemsetter.at(0));
QString nummer = QString(itemsetter.at(1));
if (nametree > letter) { /* compare alphabet position */
return nummer;
}
}
/*
original from php
$fp = fopen ("ahvkeys.csv","r");
while ($data0 = fgetcsv ($fp, 1000, ",")) {
if(strcmp ($data0[0], $name)>0) break;
$data1=$data0;
}
$first3letternummer=$data1[1];

*/

patrik08
2nd July 2006, 11:29
I solved....

strcmp is a php function & a c++ :rolleyes:

if (strcmp(ainame,yourname) > 0) { break; } register the qmap position

and take position - 1 value ... thats it... tanksss

rickbsgu
2nd July 2006, 16:21
Just a few notes on operators that you might find useful. From your original code:





TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it )
{
QStringList itemsetter = it.value();
QString letter = QString(itemsetter.at(0));
QString nummer = QString(itemsetter.at(1));
if (nametree > letter)
{
/* compare alphabet position */
return nummer;
}
}



You should be able to do this:


TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it )
{
QString letter = it[0];
QString nummer = it[1]);
if (nametree > letter)
{
/* compare alphabet position */
return nummer;
}

return QString::null; // have to return something if fallthrough


Notes:

The iterator acts as an instance of the object in the array.
You don't need to wrap the itemsetter returns in a QString() wrapper - in fact, that's kind of wasteful, since it creates a copy that you don't need (although QString does optimize copies under the covers to point to the same string. Still, unless you're going to change the copy, it's a waste.)


Taking it further:


enum ItemIndexes { LETTER, NUMBER }
// (do this somewhere)
// might want to wrap in a namespace or class somewhere, or prefix
// the items to guarantee uniqueness

TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it )
if (nametree > it[LETTER]) /* compare alphabet position */
return it[NUMBER];

return QString::null; // Have to return something if fallthrough


In fact, I think if you're using Qt4, you can use the 'foreach' keyword on iterators.

Consolidating the code makes the logic more apparent - in doing this, I discovered you weren't returning anything on fallthrough & added that.

rickb

patrik08
2nd July 2006, 17:40
Your method not run on qt4....

strcmp wand char .... a qt similar function not exist....



TableAi::Iterator it;
for ( it = Box_att.begin(); it != Box_att.end(); ++it ) {
loop++; /* register the map position to take value one before break */
if (strcmp(it[0],yourname) > 0) { break; }
}



gui_main.cpp:1069: error: cannot convert `QMapData::Node' to `const char*' for a
rgument `1' to `int strcmp(const char*, const char*)'
mingw32-make[1]: *** [build\.o\win32\gui_main.o] Error 1
mingw32-make[1]: Leaving directory `C:/_0000_current/src_datum'

jacek
2nd July 2006, 17:50
strcmp wand char .... a qt similar function not exist....
On the contrary, it does exist: http://doc.trolltech.com/4.1/qstring.html#compare