473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Processing Data using Perl Hash

48 New Member
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
9 1477
numberwhun
3,509 Recognized Expert Moderator Specialist
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
kriz4321
48 New Member
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
3,652 Recognized Expert Specialist
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
3,509 Recognized Expert Moderator Specialist
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
4,059 Recognized Expert Specialist
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
kriz4321
48 New Member
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
4,059 Recognized Expert Specialist
Post your current code.
Apr 15 '08 #8
kriz4321
48 New Member
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
971 Recognized Expert Contributor
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

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

Similar topics

2
4417
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
4
2389
by: spar | last post by:
I'm converting a Perl script to Python and have run into something I'm not sure how to do in Python. In Perl, I am running through a couple loops and inserting values directly into a complex...
5
4523
by: Jeff | last post by:
I've created a beast! Here is my data structure: $VAR1 = 'bunkers'; $VAR2 = { 'items' => , \ ] }; $VAR3 = 'simpsons'; $VAR4 = {
1
5097
by: Da-Breegster | last post by:
Hi. I'm attempting to write a roguelike (think nethack, crawl, angband, tome, adom, and yes, I suppose even rogue) in Perl and I've ran into a problem regarding the data structure for the map and...
19
3199
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
5
3212
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
6
2981
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
6
1794
by: mojo | last post by:
Hello all; I'm having trouble with my perl script and need some guidance. I am able to read data from a .csv file using the script and am placing it into an array @field. The data consists of four...
1
2179
by: rhaas | last post by:
Howdy. I've been trying to parse the return of XML::TreePP from an xBRL file using perl and Data::Dumper. The resulting Data::Dumper output looks like this: $VAR1 = { 'xbrl' =>...
5
2089
by: lilly07 | last post by:
Hi, I have a doubt regarding perl hash data. I have a tab delimited as below: file1.txt name1 AM bin1 name2 AM bin1 name3 PM bin1
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.