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

perl hash data

P: 89
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
Share this Question
Share on Google+
5 Replies


Expert Mod 100+
P: 589
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

P: 89
Hi thx fir the response...
Jan 17 '10 #3

P: 89
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

Expert Mod 100+
P: 589
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

P: 89
Thanks Ron. Thats a great help..
Jan 18 '10 #6

Post your reply

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