picking the correct row in a map <string, vector <string> >

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

picking the correct row in a map <string, vector <string> >

Pavan Shah-2
I have a map <string, vector <string> >  with hundreds of rows in it and several columns.
In other words, it looks like the following:

6567     12/20/2010    EUR  50000.58 12/20/2014
7894     12/20/2011    CAD 50000.58 12/20/2014
1237     12/20/2012    EUR  50000.58 12/20/2019
1237     12/20/2013    GBP  50000.58 12/20/2017
......................................................................
......................................................................
.......................................................................
1237     12/20/2013    GBP  50000.58 12/20/2017


the first column are my keys  but they are not in any order.  They are just random numbers stored as string.

I want to work with only rows that have a mapped value as  "EUR" in the 3rd column  and then pick out the last mapped value in that row (which is  a date) and pass this date to another function .

How do I do this ?

thanks
Pavan

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: picking the correct row in a map <string, vector <string> >

Kyle Schlansker-2
Hi Pavan,

How are you using QuantLib in your project?  It seems like the way the data is stored may be causing some headaches for you.  If you can give some background on the problem that you're trying to solve then we may be able to suggest design ideas that may not even require you to use this map / vector structure.

For this specific question I think you could do something similar to your previous parsing question.  Instead of using a vector<string> to store each of your column values, you could use a struct where each field is stored as its native type.  For example:

struct row {
   string key;
   Date d1;
   std::string currency;
   double value;
   Date d2;
};

// your map would be:
map<string, row>  data;

// and you can iterate over your map and call whatever function you need to call for each EUR date ...
for (map<string, row>::iterator it = data.begin(); it != data.end(); ++it) {
   if (it->second.currency == "EUR") {
       process_eur_date(it->second.d2);
   }
}


Or, if the only requirement is simply to process a list of dates for EUR lines, then it's probably easiest to just use unix to get the list of dates and pipe the list to your c++ program:

fgrep EUR /path/to/your/data.log | awk '{ print $NF }' | /path/to/proc-eur-dates

where proc-eur-dates.cpp is something like:

int main(int argc, char **argv) {
   Date d;
   while (std::cin >> d) {
       process_eur_date(d);
   }
   return 0;
}

-- 
Kyle Schlansker
Partner
Parametros Capital LLC
<a href="tel:978.500.0388" value="+19785000388" target="_blank">978.500.0388



On Thu, Jan 10, 2013 at 2:38 PM, Pavan Shah <[hidden email]> wrote:
I have a map <string, vector <string> >  with hundreds of rows in it and several columns.
In other words, it looks like the following:

6567     12/20/2010    EUR  50000.58 12/20/2014
7894     12/20/2011    CAD 50000.58 12/20/2014
1237     12/20/2012    EUR  50000.58 12/20/2019
1237     12/20/2013    GBP  50000.58 12/20/2017
......................................................................
......................................................................
.......................................................................
1237     12/20/2013    GBP  50000.58 12/20/2017


the first column are my keys  but they are not in any order.  They are just random numbers stored as string.

I want to work with only rows that have a mapped value as  "EUR" in the 3rd column  and then pick out the last mapped value in that row (which is  a date) and pass this date to another function .

How do I do this ?

thanks
Pavan

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Master HTML5, CSS3, ASP.NET, MVC, AJAX, Knockout.js, Web API and
much more. Get web development skills now with LearnDevNow -
350+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122812
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: picking the correct row in a map <string, vector <string> >

Pavan Shah-2
Hi Kyle,
thanks a lot for your response

I am using QuantLib in this project mostly to handle dates, calendars and also for interpolation and to gather some statistics.
My starting point is few csv files which contains my input data.  Couple of these files contain hundreds of rows (even thousands).  But that is not a problem.
As you might have guessed by now, these files contain various types of data, i.e. string, double, Date, etc.

I have been using Boost tokenizer to parse the data from these files and then storing the data into maps and in one case multimap (because this file contains history of rates for various currencies but same dates).

That being said, once the data is stored, I wish to compute a fully interpolated vol curve, compute expiry (which is easy using QuantLib), compute standard deviation using the vol and expiry (which is just a simple formula).
It  certainly seems like for the example above, I should abandon my method of storing the data  into a map <string, vector <string> >  and replace it with map < string, Struct> .  

would you agree?
thanks
Pavan

fyi,  

this is how I parse the data and store the data line by line into the current map<string, vector <string> >  .

typedef tokenizer <escaped_list_separator <char> > Tokenizer;

vector <string> vec;
//the actual data structure
map<string,vector<string> > testmap;

while (getline(Ports,line))
{
Tokenizer tok(line);
vec.assign(tok.begin(),tok.end());

string key = vec[0];
vector <string>row;
/*vector <string>::iterator start = row.begin()+1, end =   row.end();*/
for (int i=1;i<vec.size();i++)
{
row.push_back(vec[i]);
}


//put the sample data into the map
testmap[key]=row;
//output the data from the map
//cout<<testmap[key].at(0)<<" " << testmap[key].at(1)<<" " << testmap[key].at(12)<<endl;  // There are a total of 13 mapped values to a single trade it in this file.

/*if (vec.size() < 3)  continue;*/

/*copy (vec.begin(),vec.end(), ostream_iterator<string> (cout,"  "));*/
}


On Thu, Jan 10, 2013 at 8:55 PM, Kyle Schlansker <[hidden email]> wrote:
Hi Pavan,

How are you using QuantLib in your project?  It seems like the way the data is stored may be causing some headaches for you.  If you can give some background on the problem that you're trying to solve then we may be able to suggest design ideas that may not even require you to use this map / vector structure.

For this specific question I think you could do something similar to your previous parsing question.  Instead of using a vector<string> to store each of your column values, you could use a struct where each field is stored as its native type.  For example:

struct row {
   string key;
   Date d1;
   std::string currency;
   double value;
   Date d2;
};

// your map would be:
map<string, row>  data;

// and you can iterate over your map and call whatever function you need to call for each EUR date ...
for (map<string, row>::iterator it = data.begin(); it != data.end(); ++it) {
   if (it->second.currency == "EUR") {
       process_eur_date(it->second.d2);
   }
}


Or, if the only requirement is simply to process a list of dates for EUR lines, then it's probably easiest to just use unix to get the list of dates and pipe the list to your c++ program:

fgrep EUR /path/to/your/data.log | awk '{ print $NF }' | /path/to/proc-eur-dates

where proc-eur-dates.cpp is something like:

int main(int argc, char **argv) {
   Date d;
   while (std::cin >> d) {
       process_eur_date(d);
   }
   return 0;
}

-- 
Kyle Schlansker
Partner
Parametros Capital LLC
<a href="tel:978.500.0388" value="+19785000388" target="_blank">978.500.0388



On Thu, Jan 10, 2013 at 2:38 PM, Pavan Shah <[hidden email]> wrote:
I have a map <string, vector <string> >  with hundreds of rows in it and several columns.
In other words, it looks like the following:

6567     12/20/2010    EUR  50000.58 12/20/2014
7894     12/20/2011    CAD 50000.58 12/20/2014
1237     12/20/2012    EUR  50000.58 12/20/2019
1237     12/20/2013    GBP  50000.58 12/20/2017
......................................................................
......................................................................
.......................................................................
1237     12/20/2013    GBP  50000.58 12/20/2017


the first column are my keys  but they are not in any order.  They are just random numbers stored as string.

I want to work with only rows that have a mapped value as  "EUR" in the 3rd column  and then pick out the last mapped value in that row (which is  a date) and pass this date to another function .

How do I do this ?

thanks
Pavan

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122712
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Master HTML5, CSS3, ASP.NET, MVC, AJAX, Knockout.js, Web API and
much more. Get web development skills now with LearnDevNow -
350+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122812
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users