By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Processing Data using Perl Hash

P: 48
Hi all,
I have a data similar to one listed below,(It is stored in a array with comma as delimiter).
For each month and each Department I need to get the status count. I thought of using Hashes but not sure how to get it.

Sampe Input:
ID Status Depar DATE
D123 Good A 12-03-2008
D123 Bad B 13-04-2008
C123 Poor A 12-03-2008
F123 Good B 15-05-2008
V123 Good E 14-04-2008
V123 Poor A 13-02-2008
V123 GOOD A 13-03-2008

Output:
Enter the dept: A ( From User)
for the Month -03
GOOD:2
BAD:0
POOR:1
for the Month -04
GOOD:1
BASD:1
POOR:0

Similarly I need to get this for all the month. Any pointers that can be used to solve will be extremely helpfull...
Thank in Advnace
Apr 14 '08 #1
Share this Question
Share on Google+
9 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi all,
I have a data similar to one listed below,(It is stored in a array with comma as delimiter).
For each month and each Department I need to get the status count. I thought of using Hashes but not sure how to get it.

Sampe Input:
ID Status Depar DATE
D123 Good A 12-03-2008
D123 Bad B 13-04-2008
C123 Poor A 12-03-2008
F123 Good B 15-05-2008
V123 Good E 14-04-2008
V123 Poor A 13-02-2008
V123 GOOD A 13-03-2008

Output:
Enter the dept: A ( From User)
for the Month -03
GOOD:2
BAD:0
POOR:1
for the Month -04
GOOD:1
BASD:1
POOR:0

Similarly I need to get this for all the month. Any pointers that can be used to solve will be extremely helpfull...
Thank in Advnace
I don't think you need to go to the extent of using hashes for this, especially if you have each element of your array each containing one of the sample lines (which has all the information you need.

What I would do is simply read in the elements, one by one, then process them for what I needed and format the output after that.

Since this is a learning forum, why not try it and if you get stuck, show us your code and we will try and help you along. Please know that if you do need help, you will need to post your code here, enclosed in code tags.

Regards,

Jeff
Apr 14 '08 #2

P: 48
The contents of the file is huge.(Around 750 entry) It has many departments also. Not sure if processing them line by line will be helpfull.
Need to decide the logic before starting...Please let me know your views..
Apr 14 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
Processing them line by line will be fine, and I'm not sure there's another way to get all the contents of the file.

Your best option would be to read in a line (make sure it's an input line, not the header line telling a reader what data is stored), then use a regular expression to capture the dates/status etc. etc. Once you have separated this information, use whatever you want to store the information - a series of hashes for each data element (date, status, etc.) holding the count as the value and the name as the key (for example, GOOD => 2) would be an elegant solution, provided you know all the statuses are all uppercase/all lowercase/in the same format.
Apr 14 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
The contents of the file is huge.(Around 750 entry) It has many departments also. Not sure if processing them line by line will be helpfull.
Need to decide the logic before starting...Please let me know your views..
Well, that depends on how you look at the grand scheme of things. If the sample data is pretty accurate as to the size of each entry, then the longest line looks to be about 22 Bytes in length. You multiply that times 750 and that works out to about 17K worth of data. Most machines these days have a lot more than 17K of memory, I promise you, unless you are working on such an arcane beast that nobody else would want to touch it.

What I am saying is, unless you are talking about many thousands of lines, this shouldn't be a problem.

As for the format of the input data, is what you have above pretty close? Is the first line always that column titles?

Granted, others here may have other ideas and feel free to use them as you see fit, this is just my approach to the issue.

Regards,

Jeff
Apr 14 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
The contents of the file is huge.(Around 750 entry) It has many departments also. Not sure if processing them line by line will be helpfull.
Need to decide the logic before starting...Please let me know your views..

You will have to process them line by line, or if they are already in an array, element by element. There is no getting around that.
Apr 14 '08 #6

P: 48
Thanks. I have processed line by line for the given department and pushed the contents in an array. When I print the array it looks as follows ( state Month)

GOOD 03 BAD 04 GOOD 03 POOR 04 BAD 02 BAD 03 GOOD 05 BAD 06

Now I need to count for each month(03,04,05,05,06 which are in the array)..

eg: for 03
GOOD:3
BAD:01
POOR:0

Hash wiil be useful for this. I have not worked on hashes any pointers for the same will be helpfull.
Apr 15 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
Post your current code.
Apr 15 '08 #8

P: 48
This is the code I have so far...
The output of array will look like this

Expand|Select|Wrap|Line Numbers
  1. GOOD 03 BAD 04 GOOD 03 POOR 04 BAD 02 BAD 03 GOOD 05 BAD 06
Expand|Select|Wrap|Line Numbers
  1. open(FH,"sample.txt");
  2. my @month  = qw(Jan Feb mar Apr may jun Jul Aug Sep OCt Nov Dec);
  3. print "Please Enter the Input Qu";
  4. $temp=<STDIN>;
  5. chop($temp);
  6. print "Please Enter the Input Dep";
  7. $IDEPT=<STDIN>;
  8. chop($IDEPT);
  9. while(<FH>)
  10. {
  11. chomp;
  12. if(($temp=~/q1/) && ($_=~/(\d\d)-(\d\d)-(\d\d\d\d)/)) 
  13. {
  14.  
  15.   if(($2 >= 04) && ($2 <=07) && ($_=~/$IDEPT/))
  16.   {
  17.  
  18.     ($ID,$DEPT,$STATE,$DATE)=split ',';
  19.      $Month = substr ($DATE, 3, 2);
  20.      #push @qua1,$DEPT;
  21.      push @qua1,$STATE;
  22.      push @qua1,$Month;     
  23.   } 
  24.  
  25. }
  26.  
  27. }
  28. print "@qua1\n";
  29. close(FH);
Apr 16 '08 #9

eWish
Expert 100+
P: 971
This is basically an illustration on how to use a hash to store the data that then access it. You will need to make changes to make it work for your exact needs. There are other ways of handling this but it will help you get started with hashes.....I hope.

Expand|Select|Wrap|Line Numbers
  1. my %data_hash = ();
  2. my $file = 'f:/test_file.txt';
  3.  
  4. open(my $FILE, '<', $file) || die "Can't open file $file: $!\n";
  5. while (<$FILE>) {
  6.     chomp;
  7.  
  8.     my ($id, $status, $dept, $date) = split(/ /);
  9.     my ($day, $month, $year) = split(/\-/, $date);
  10.     my $formatted_date = $month . '-' . $year;
  11.  
  12.     $data_hash{$formatted_date}->{$dept}->{$status}++ ;
  13.  
  14. }
  15. close($FILE);
  16.  
  17. foreach my $date (keys %data_hash) {
  18.     print 'Month / Date: ', $date, "\n";
  19.  
  20.     foreach my $dept( keys %{$data_hash{$date}}) {
  21.         print 'Dept: ', $dept, "\n";
  22.  
  23.         foreach my $status (keys %{$data_hash{$date}->{$dept}}) {
  24.             print $status, ' = ',  $data_hash{$date}->{$dept}->{$status}, "\n";
  25.         }
  26.     }
  27.     print "\n";
  28. }
--Kevin
Apr 16 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.