Connecting Tech Pros Worldwide Forums | Help | Site Map

Parsing a Text File

Newbie
 
Join Date: Feb 2007
Posts: 2
#1: Feb 19 '07
I have this data inside a file saved as “filename.txt” with bunch of other data what I want to do is I want to sort the following data and have an output as shown below

These lines are inside my text file:

number_of_students................ Total_by_gender 25 (F 11 => 44.0) (M 14 => 56.0)

number_of_students................ Total_by_gender 73 (F 69 => 94.5) (M 4 => 5.5)

number_of_students................ Total_by_gender 14 (F 1 => 7.1) (M 13 => 92.9)



the Out put I want :

Total number of Female = 11+69+1 = 81
Total number of male = 14+4+13 = 31

Total number of student = 25+73+14 = 112

The number after => is percentage and I want to calculate that as well

I would really appreciate if some one can give me some ideas and help me as well.


Thank You
T_Lee

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Feb 19 '07

re: Parsing a Text File


What have you tried so far? Any code at all?
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#3: Feb 20 '07

re: Parsing a Text File


@ebmt2006

I don't know for what I should need code from you?

So, here we go (just to help you, as requested):
Expand|Select|Wrap|Line Numbers
  1. @lines = (
  2.   "number_of_students................ Total_by_gender 25 (F 11 => 44.0) (M 14 => 56.0)",
  3.   "number_of_students................ Total_by_gender 73 (F 69 => 94.5) (M 4 => 5.5)",
  4.   "number_of_students................ Total_by_gender 14 (F 1 => 7.1) (M 13 => 92.9)",
  5. );
  6.  
  7. for $line ( @lines ) {
  8.   ( $f, $m ) = ($line =~ /\(F\s+(\d+)\s+=>\s+[\d.]+\)\s*\(M\s+(\d+)\s+=>\s+[\d.]+\)/);
  9.   push(@fem, $f);
  10.   push(@male, $m);
  11. }
  12.  
  13. $f = join('+', @fem);
  14. $m = join('+', @male);
  15.  
  16. printf("Total number of female: %s = %d\n", $f, eval($f));
  17. printf("Total number of   male: %s = %d\n", $m, eval($m));
Greetz, Doc
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Feb 20 '07

re: Parsing a Text File


Quote:
I don't know for what I should need code from you?
A smart guy like you can probably think of a few reasons. ;)
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#5: Feb 20 '07

re: Parsing a Text File


Quote:

Originally Posted by KevinADC

A smart guy like you can probably think of a few reasons. ;)

No, I can't!

I just prefer to give a simple answer to a simple question.

I am wrong to do so?

Greetz, Doc
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#6: Feb 20 '07

re: Parsing a Text File


Addition to Kevin:

Sorry, I did not want to offend you.

I just paid attention to the question, in which the concern was clearly stated:
Expand|Select|Wrap|Line Numbers
  1. I would really appreciate if some one can give me some ideas and help me as well.
A forum like this is intended to interchange experiences among experts and especially to transfere knowledge from experts to novices. In most cases, no novice gets help, if he is confronted with a counterquestion. This might be helpful in some circumstances (e.g. if the problem is not described clearly), but if a direct answer is possible, it should be given. That's my opinion and as my personal experience has shown, novices appreciate this philosophy.

Greetz, Doc
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#7: Feb 20 '07

re: Parsing a Text File


Quote:

Originally Posted by docsnyder

Addition to Kevin:

Sorry, I did not want to offend you.

I just paid attention to the question, in which the concern was clearly stated:

Expand|Select|Wrap|Line Numbers
  1. I would really appreciate if some one can give me some ideas and help me as well.
A forum like this is intended to interchange experiences among experts and especially to transfere knowledge from experts to novices. In most cases, no novice gets help, if he is confronted with a counterquestion. This might be helpful in some circumstances (e.g. if the problem is not described clearly), but if a direct answer is possible, it should be given. That's my opinion and as my personal experience has shown, novices appreciate this philosophy.

Greetz, Doc


OK. My experience and philosophy is different than yours on the subject.
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#8: Feb 20 '07

re: Parsing a Text File


Quote:

Originally Posted by KevinADC

OK. My experience and philosophy is different than yours on the subject.

Definitely!!!

Greetz, Doc
Reply