473,651 Members | 3,011 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 1482
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...Plea se 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...Plea se 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...Plea se 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
4431
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 sorted columns. to do this at the moment i am using perl, which is very easy to do, and i want to see if python is as easy. so, the data i am using is some epiphyte population abundance data for particular sites, and it looks like this:
4
2401
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 data structure (array->hash->array). This isn't the actual code, but should demonstrate the general idea: foreach $bar_count(@bars) { foreach $el_count(@els) { $var = somefunc($bar_count,$el_count);
5
4530
by: Jeff | last post by:
I've created a beast! Here is my data structure: $VAR1 = 'bunkers'; $VAR2 = { 'items' => , \ ] }; $VAR3 = 'simpsons'; $VAR4 = {
1
5104
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 handling it easily. A map is one 2D level, a simple grid. Each tile on the grid will be a hash of data about the grid. The map object itself has other bits of data, so that's why I don't just make the object an anonymous array: $map->{Map} = {...
19
3211
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 first position is the info (the product) I want to retreive for the corresponding code. Assuming that the codes are unique for each product and all code data is on one line.
5
3222
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: someArray = 6; I know I can't get the same syntactic sugar as Perl offers, with the usage of a string as the array key surrounded by square brackets. I just want the general functionality, that's all. That is, a data container that will maintain...
6
2994
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 for one of the Pear modules for creating XML and it didn't look like much. I've used Perl XML:Twig and I have the impression that there is more Perl stuff available as well as the Perl stuff being well documented as I have a Perl DBI book, a Perl...
6
1797
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 columns. I'm using the first column $field and the fourth column $field, and placing that into a hash. My problem is that I want to be able to use the data in $field, to reference the keys in the hash. $field consists of number 1 - 4, and...
1
2196
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' => { 'dow:ComprehensiveIncomeloss' => , 'dow:DistributionsFromNonconsolidatedAffiliates' =>
5
2093
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
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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 we have to send another system

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.