extracting a report from files

G

Gary Wessle

Hi

I would appreciate any idea on how to approach this task.
I have files which look like this:

file1
men 234
women 112
children 4
cars 11


file2
men 344
children 34
dogs 9

....

I need to print a report of all the data in the files which look like
this.

files men women chidden cars dogs

file1 234 112 4 11 0
file2 344 0 34 0 9

that is a table with fields names are columns and file names are
rows, the columns name all possible categories in the files, raw names
the files, a zero where there is no data for that particular field in
any given file.

I was thinking, open files, read data into a map, iterate through the
map, depending on the key, place the value in vector so that we have
vector for men, and another for women,... with the size of "number of
files", then foreach vector<category> (foreach vector<files>) cout
with setw(int) and so forth.



thanks
 
M

Marcus Kwok

Gary Wessle said:
Hi

I would appreciate any idea on how to approach this task.
I have files which look like this:

file1
men 234
women 112
children 4
cars 11


file2
men 344
children 34
dogs 9

...

I need to print a report of all the data in the files which look like
this.

files men women chidden cars dogs

file1 234 112 4 11 0
file2 344 0 34 0 9

that is a table with fields names are columns and file names are
rows, the columns name all possible categories in the files, raw names
the files, a zero where there is no data for that particular field in
any given file.

I was thinking, open files, read data into a map, iterate through the
map, depending on the key, place the value in vector so that we have
vector for men, and another for women,... with the size of "number of
files", then foreach vector<category> (foreach vector<files>) cout
with setw(int) and so forth.

I would do something like,

typedef std::map<std::string, int> Category;
std::map<std::string, Category> files;

Then, once you fill it, you can access it like:

std::cout << files["file1"]["men"] << '\n';

Filling in the maps and converting this to use iterators is left as an
exercise.
 
J

Jim Langston

Gary Wessle said:
Hi

I would appreciate any idea on how to approach this task.
I have files which look like this:

file1
men 234
women 112
children 4
cars 11


file2
men 344
children 34
dogs 9

...

I need to print a report of all the data in the files which look like
this.

files men women chidden cars dogs

file1 234 112 4 11 0
file2 344 0 34 0 9

that is a table with fields names are columns and file names are
rows, the columns name all possible categories in the files, raw names
the files, a zero where there is no data for that particular field in
any given file.

I was thinking, open files, read data into a map, iterate through the
map, depending on the key, place the value in vector so that we have
vector for men, and another for women,... with the size of "number of
files", then foreach vector<category> (foreach vector<files>) cout
with setw(int) and so forth.

Your way sounds about as good as any other. This could be done a few
different ways, but I see nothing wrong with the way you plan on going about
it.

One thing I might add, though, is I might also use a set that just contained
the key names (men, women, etc..).

Read a file, read the key and the value. Place the Key into a set. Place
the key and value into a map (a vector of maps so you could read however
many files).

When you are finished processing files, iterate through the map outputing
the keys (so you get your files, men, women, children, cars, dogs, etc..).
Then iteratore through the set for each map in the vector finding each key
and outputing the value, 0 if it is not found.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top