Processing Data using Perl Hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kriz4321
    New Member
    • Jan 2007
    • 48

    Processing Data using Perl Hash

    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
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by kriz4321
    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

    Comment

    • kriz4321
      New Member
      • Jan 2007
      • 48

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

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

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

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by kriz4321
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by kriz4321
            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.

            Comment

            • kriz4321
              New Member
              • Jan 2007
              • 48

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

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Post your current code.

                Comment

                • kriz4321
                  New Member
                  • Jan 2007
                  • 48

                  #9
                  This is the code I have so far...
                  The output of array will look like this

                  Code:
                  GOOD 03 BAD 04 GOOD 03 POOR 04 BAD 02 BAD 03 GOOD 05 BAD 06
                  [CODE=perl]open(FH,"sample .txt");
                  my @month = qw(Jan Feb mar Apr may jun Jul Aug Sep OCt Nov Dec);
                  print "Please Enter the Input Qu";
                  $temp=<STDIN>;
                  chop($temp);
                  print "Please Enter the Input Dep";
                  $IDEPT=<STDIN>;
                  chop($IDEPT);
                  while(<FH>)
                  {
                  chomp;
                  if(($temp=~/q1/) && ($_=~/(\d\d)-(\d\d)-(\d\d\d\d)/))
                  {

                  if(($2 >= 04) && ($2 <=07) && ($_=~/$IDEPT/))
                  {

                  ($ID,$DEPT,$STA TE,$DATE)=split ',';
                  $Month = substr ($DATE, 3, 2);
                  #push @qua1,$DEPT;
                  push @qua1,$STATE;
                  push @qua1,$Month;
                  }

                  }

                  }
                  print "@qua1\n";
                  close(FH);[/CODE]
                  Last edited by eWish; Apr 16 '08, 02:10 PM. Reason: Please use code tags

                  Comment

                  • eWish
                    Recognized Expert Contributor
                    • Jul 2007
                    • 973

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

                    [CODE=perl]my %data_hash = ();
                    my $file = 'f:/test_file.txt';

                    open(my $FILE, '<', $file) || die "Can't open file $file: $!\n";
                    while (<$FILE>) {
                    chomp;

                    my ($id, $status, $dept, $date) = split(/ /);
                    my ($day, $month, $year) = split(/\-/, $date);
                    my $formatted_date = $month . '-' . $year;

                    $data_hash{$for matted_date}->{$dept}->{$status}++ ;

                    }
                    close($FILE);

                    foreach my $date (keys %data_hash) {
                    print 'Month / Date: ', $date, "\n";

                    foreach my $dept( keys %{$data_hash{$d ate}}) {
                    print 'Dept: ', $dept, "\n";

                    foreach my $status (keys %{$data_hash{$d ate}->{$dept}}) {
                    print $status, ' = ', $data_hash{$dat e}->{$dept}->{$status}, "\n";
                    }
                    }
                    print "\n";
                    }[/CODE]
                    --Kevin

                    Comment

                    Working...