getting a average value in Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpjacquez
    New Member
    • Oct 2008
    • 5

    getting a average value in Perl

    There are 4 fields in this input data. I want to get the average value for the column 2/3/4th. If you notice, disk c53t2d6 and c12t10d4 occured twice..so i need to get an average for them. If it occured only once, then just get take it as an average.

    c42t3d0 0.01 0.00 1.19
    c53t2d6 2.00 3.4 1.76
    c12t10d4 0.01 0.00 1.42
    c53t2d6 0.03 0.00 0.59
    c12t10d4 0.02 0.00 0.82

    THanks in advance. I been banging my head for a 3 days now..but no luck..Don't know how to structure it..


    Jewsco
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    That's all well and good, but what code have you produced? We need to see what you have tried before we can help you with it.

    Regards,

    Jeff

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      I'm thinking a hash, with the cXXXXX string as the key and an array reference as the value, with the array containing the numbers following it. Calculating the average, then, is fairly easy. It'll take some tweaking, but I'm sure you can do it.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by Ganon11
        I'm thinking a hash, with the cXXXXX string as the key and an array reference as the value, with the array containing the numbers following it. Calculating the average, then, is fairly easy. It'll take some tweaking, but I'm sure you can do it.
        I agree.

        Comment

        • jpjacquez
          New Member
          • Oct 2008
          • 5

          #5
          here is the code, actually, its just opening and puting it into a hash. I stoped because i not sure how to proceed..

          Code:
          open(SAROUT,"/tmp/input_file" ) or die "Can't open $!";
          
          while(<SAROUT>) {
             chomp;
             s/\s+/ /g; s/^\s+//g;
             $Lines++;
          
             if ( $FoundBegin eq 1 ) {
               print "Lines in between: $_\n";
               ( $Disk, $DiskBusy, $Avque, $Rws, $Blks, $DiskWaitIO, $DiskSvcTime ) = split / /, $_;
               $Disks{$Lines}{'disk'}=$Disk;
               $Disks{$Lines}{'busy'}=$DiskBusy;
               $Disks{$Lines}{'waitio'}=$DiskWaitIO;
               $Disks{$Lines}{'svctime'}=$DiskSvcTime;
          
             }
          
             if ( /$BeginTime/ )  {
               $Lines=0;
               print "Begin Line: $_\n";
               ( $SarTime, $Disk, $DiskBusy, $Avque, $Rws, $Blks, $DiskWaitIO, $DiskSvcTime ) = split / /, $_;
               $Disks{$Lines}{'disk'}=$Disk;
               $Disks{$Lines}{'busy'}=$DiskBusy;
               $Disks{$Lines}{'waitio'}=$DiskWaitIO;
               $Disks{$Lines}{'svctime'}=$DiskSvcTime;
               $FoundBegin=1;
          
          
             } elsif ( /$EndTime/ ) {
               $Entries[$Lines]=$_;
               print "End Line: $Lines\n";
               close(SAROUT);
             }
          Last edited by eWish; Oct 15 '08, 11:07 PM. Reason: Please use code tags

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            That code seems to have no relation to the data you posted previoulsy:

            c42t3d0 0.01 0.00 1.19
            c53t2d6 2.00 3.4 1.76
            c12t10d4 0.01 0.00 1.42
            c53t2d6 0.03 0.00 0.59
            c12t10d4 0.02 0.00 0.82

            Your code splits the lines into 8 fields of data, but the data you posted only has 4 fields (assumes space delimited). So whats wrong? The code or the data?

            Comment

            • jpjacquez
              New Member
              • Oct 2008
              • 5

              #7
              sorry for confusion..repl ace the open with..The begin and End pattern is 00:10:00 and 00:20:00 for example..

              Code:
              open(SAROUT,"/usr/bin/sar -d -f $SarFile |" ) or die "Can't open $!";

              output will be like
              HP-UX cmihx195 B.11.23 U ia64 10/12/08

              Code:
              00:00:00   device   %busy   avque   r+w/s  blks/s  avwait  avserv
              00:10:00   c2t6d0   10.85    1.43      31     755    1.90    9.74
                         c6t6d0    8.75    1.51      26     622    1.94    8.71
                        c8t12d7    0.01    0.50       0       0    0.00    2.25
                         c9t8d2    0.03    0.50       0       4    0.00    3.46
                         c9t0d1    0.14    0.50       0      23    0.00    7.60
                        c12t1d0    8.91    0.50       6      90    0.00   16.29
                        c12t1d1   12.21    0.50       6     103    0.00   19.11
                        c12t1d7    0.00    0.50       0       0    0.00    0.85
                        c12t1d2   11.73    0.50       6      98    0.00   19.28
                        c12t2d1    0.08    0.50       0      10    0.00   16.39
                        c12t2d0    0.09    0.50       0      14    0.00   26.69
                        c12t2d5    0.12    0.50       0      14    0.00   19.99
              00:20:00   c2t6d0    6.20    0.69      14     151    0.56    9.50
                         c6t6d0    4.82    0.73      12     144    0.68    7.54
                        c8t12d7    0.01    0.50       0       0    0.00    3.03
                         c9t8d2    0.03    0.50       0       4    0.00    2.48
                         c9t0d1    0.02    0.50       0       1    0.00    1.54
                        c12t1d0    7.55    0.50       5      72    0.00   16.72
                        c12t1d1    8.59    0.50       5      73    0.00   18.82
                        c12t1d2   10.49    0.50       6      91    0.00   18.56
                        c12t2d1    0.00    0.50       0       0    0.00    1.18


              Thansk a lot!
              Last edited by eWish; Oct 15 '08, 11:08 PM. Reason: Please use code tags

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Twice you have posted code, and twice we have added code tags for you. Please be mindful of your posting and add code tags around any and all code. They are easy to use and quite necessary.

                Regards,

                Jeff

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  You're making it too hard. Maybe someone else will take the time to try and figure out what you want.

                  Comment

                  • jpjacquez
                    New Member
                    • Oct 2008
                    • 5

                    #10
                    if you notice my first question, i made it simple actually. get the average for each occurence of disks. i didn't want to put the code as it may confuse you.. but somebody asked for it..

                    making it hard? yes its easy for you...thats the reason i asked your help in the forum..because i don't know how to make it work..

                    thanks,
                    jewsco

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      OK, sticking with your original sample data and assuming each field is averaged seperately:

                      Code:
                      #use Data::Dumper;
                      my %HoA = ();
                      while (<DATA>){
                         chomp;
                         my @data = split(/\s+/);
                         for my $i (1..3){
                            $HoA{$data[0]}[$i-1]+=$data[$i];
                         }
                         $HoA{$data[0]}[3]++;
                      }
                      #print Dumper \%HoA;
                      foreach my $key (keys %HoA) {
                         print "Average for $key: ";
                         foreach my $i ( 0..$#{$HoA{$key}}-1 ) {
                            if ($HoA{$key}[$i] == 0) {
                               print "0 ";
                            }
                            else {
                               my $s = sprintf  "%.4f", $HoA{$key}[$i] / $HoA{$key}[3];
                               print "$s ";
                            }
                         }
                         print "\n";
                      }
                      __DATA__
                      c42t3d0 0.01 0.00 1.19
                      c53t2d6 2.00 3.4 1.76
                      c12t10d4 0.01 0.00 1.42
                      c53t2d6 0.03 0.00 0.59
                      c12t10d4 0.02 0.00 0.82
                      This could be done a number of different ways so I am not in anyway suggesting my code is the best way to go about this. Hopefully it helps you extrapolate something you can apply to your real data and your needs.

                      Regards,
                      Kevin

                      Comment

                      • jpjacquez
                        New Member
                        • Oct 2008
                        • 5

                        #12
                        WOW! Thank you very much Kevin! Appreciate it man..

                        I didn't know this technique.

                        Code:
                         
                        
                        $HoA{$data[0]}[$i-1]+=$data[$i];

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          You're welcome. I hope it helps.

                          Comment

                          Working...