perl hash data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    perl hash data

    Hi,

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

    file1.txt

    Code:
    name1   AM   bin1
    name2   AM   bin1
    name3   PM   bin1
    name4   AM   bin2
    name5   AM   bin1
    name6   PM   bin2
    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.

    Code:
    {key}     {value}
    bin1      AM,AM,PM,AM
    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
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    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.

    Comment

    • lilly07
      New Member
      • Jul 2008
      • 89

      #3
      Hi thx fir the response...

      Comment

      • lilly07
        New Member
        • Jul 2008
        • 89

        #4
        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.

        Code:
        while ( <> )
        
        {
                 chomp $_;
                @v = split(/\t/,$_);
                $bin = $v[2];
        
                 $key = $v[1];
                $value = $v[1];
                 $HoH{$bin}{$key} = $value;
        }
        
        
        foreach $bin ( keys %HoH ) {
                 print "$bin: { ";
                for $time ( keys %{ $HoH{$bin} } ) {
                 print "$time=$HoH{$bin}{$time} ";
                }
                print "}\n";
        }
        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.

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          Try this:
          Code:
          #!/usr/bin/perl
          
          use strict;
          use warnings;
          use Data::Dumper;
          
          my %binHoH;
          
          while (my $line = <DATA>) {
             chomp $line;
          
             my ($name, $time, $bin) = split(/\t/, $line);
          
             $binHoH{$bin}{$time}++;
          }
          print Dumper \%binHoH;
          
          foreach my $bin ( sort keys %binHoH ) {
             printf("%s %dAM %dPM\n", $bin, $binHoH{$bin}{AM}, $binHoH{$bin}{PM});
          }
          
          __DATA__
          name1	AM	bin1
          name2	AM	bin1
          name3	PM	bin1
          name4	AM	bin2
          name5	AM	bin1
          name6	PM	bin2

          Comment

          • lilly07
            New Member
            • Jul 2008
            • 89

            #6
            Thanks Ron. Thats a great help..

            Comment

            Working...