473,473 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

perl hash data

89 New Member
Hi,

I have a doubt regarding perl hash data. I have a tab delimited as below:

file1.txt

Expand|Select|Wrap|Line Numbers
  1. name1   AM   bin1
  2. name2   AM   bin1
  3. name3   PM   bin1
  4. name4   AM   bin2
  5. name5   AM   bin1
  6. name6   PM   bin2
  7.  
My objective would be to count the anumber of AM and PM according to the bin number as below:


bin1 3AM 1PM
bin2 1AM 1PM


I am planning to collect the data in a hash as below: With a comma separating each vale.

Expand|Select|Wrap|Line Numbers
  1. {key}     {value}
  2. bin1      AM,AM,PM,AM
  3. bin2      AM,PM
I know how to collect the data into a hash as above. I am not able to post my preliminary code as I am not in office right now.

My doubt is after collecting the data into a hash as shown above, how do I count how many AMs and PMs are in each record?

Please let me know.
Regards
Jan 15 '10 #1
5 2087
RonB
589 Recognized Expert Moderator Contributor
Use a HoH (Hash of Hashes). The first level keys are the bin numbers and the second level keys are 'AM' and 'PM', and their value is the count.
Jan 16 '10 #2
lilly07
89 New Member
Hi thx fir the response...
Jan 17 '10 #3
lilly07
89 New Member
Hi to be frank,
I have not used perl HoH before and still confusing after seeing some examples. I tried as beolw and am not able to achieve as I said above.

Expand|Select|Wrap|Line Numbers
  1. while ( <> )
  2.  
  3. {
  4.          chomp $_;
  5.         @v = split(/\t/,$_);
  6.         $bin = $v[2];
  7.  
  8.          $key = $v[1];
  9.         $value = $v[1];
  10.          $HoH{$bin}{$key} = $value;
  11. }
  12.  
  13.  
  14. foreach $bin ( keys %HoH ) {
  15.          print "$bin: { ";
  16.         for $time ( keys %{ $HoH{$bin} } ) {
  17.          print "$time=$HoH{$bin}{$time} ";
  18.         }
  19.         print "}\n";
  20. }
  21.  
  22.  
As Ron suggested, my first level of key is the bin number and the second level of key is the AM or PM.
But definitely something is wrong and am not able to count correctly. Please let me know. Thanks.
Jan 17 '10 #4
RonB
589 Recognized Expert Moderator Contributor
Try this:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my %binHoH;
  8.  
  9. while (my $line = <DATA>) {
  10.    chomp $line;
  11.  
  12.    my ($name, $time, $bin) = split(/\t/, $line);
  13.  
  14.    $binHoH{$bin}{$time}++;
  15. }
  16. print Dumper \%binHoH;
  17.  
  18. foreach my $bin ( sort keys %binHoH ) {
  19.    printf("%s %dAM %dPM\n", $bin, $binHoH{$bin}{AM}, $binHoH{$bin}{PM});
  20. }
  21.  
  22. __DATA__
  23. name1    AM    bin1
  24. name2    AM    bin1
  25. name3    PM    bin1
  26. name4    AM    bin2
  27. name5    AM    bin1
  28. name6    PM    bin2
Jan 17 '10 #5
lilly07
89 New Member
Thanks Ron. Thats a great help..
Jan 18 '10 #6

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

Similar topics

2
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...
2
by: scadav | last post by:
I am new to Perl and I am trying for figure out how to solve this problem. If anyone can give me some suggestions, I would greatly appreciate it. I am trying to read a log file and generate...
5
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an...
5
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: ...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
5
by: Karyn Williams | last post by:
I am new to Pyton. I am trying to modify and understand a script someone else wrote. I am trying to make sense of the following code snippet. I know line 7 would be best coded with regex. I first...
2
by: muppetjones | last post by:
I have been receiving a "Floating point exception" from Perl after my program has run several iterations. I have not been able to pin point the location of the error, and I am clueless. Especially as...
11
by: doraima29 | last post by:
Hi, I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side...
4
by: lilly07 | last post by:
Hi, I have two text file as below: file1.txt (4 columns) test1 1000 2000 + test2 1000 2000 - test1 1000 2000 + test3 1000 2000 + test1 1000 2000 -
0
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,...
1
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...
0
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.