PDA

View Full Version : How to get a value in a string list to match up with multiple indeces?



gamenovice
8th May 2012, 01:11
Hello all,

suppose I had 4 stringlists for a store: Itemlist, Patronlist, DateBoughtList, DateReturnedList.


And suppose I had it lined up like this:


INDECES: 0 1 2 3 4

Item: LoTR HarryPotter 007 MarioKart Clothes
Patron: Bob Sally Bob Bob Frank
DateBought: march june july july august
DateReturned: sept. sept oct dec november


I need to get a report that organizes this data by patron ( where each UNIQUE patron gets his/her own page with the appropriate items sorted out on)

This is not a printing question, but what I was wondering is how can I group together the patron elements into unique elements, letting these elements retain multiple indeces (I would suspect QMultiHash, but am not too familiar with the API at this point.... still new at it)

For example, if I were to isolate Bob: he has indeces 0,2, and 3. This would allow me to organize the data by printing out those lists at those indices on his patron page. Like so:



Patron Bob(unique element 1):

**note that the items displayed on his page I am also trying to keep sorted by item for the program
(item),(date bought),(date returned)

007, july, oct
LoTR..
MarioKart..

Patron Frank(unique element 2)

clothes,..

etc.


based off of this, is there any way to make it so I can match a patron value with multiple indeces while being organized enough to conduct a report?

***if I am not being clear, let me know

ChrisW67
8th May 2012, 04:57
You are clear enough, but perhaps you are approaching this the wrong way. You have four pieces of information about each transaction that must always be together to make sense. This is a classic case for a struct or class rather than trying to keep separate lists in sync:


struct Txn {
QString patron;
QString item;
QDate dateOut;
QDate dateIn;
// you could add a constructor to make creating these easier
};

QMultiHash<QString, Txn> data; // map patron name to one or more transactions
// or use QMultiMap if sorting by name is useful

// Add a transaction
Txn txn;
tx.patron = "Bob";
txn.item = "Slinky";
txn.dateOut = QDate(2012, 1, 1);
txn.dateIn = QDate(2012, 2, 1);
data.insert(txn.patron, txn);

// Access all transactions by Bob
if (data.contains("Bob")) {
foreach(const Txn &txn, data.values("Bob")) {
qDebug() << tx.patron << txn.item << txn.dateOut << txn.dateIn;
}
}


This sort of data does lend itself to a relational database as an alternative.

gamenovice
9th May 2012, 06:18
this helps for the most part, but i am not searching for a specific person, i am trying to iterate through all unique elements to put on my report

ChrisW67
9th May 2012, 08:25
I am sure if you read the QMultiHash/QHash or QMultiMap/QMap docs you will find code snippets showing different ways to iterate over the data.