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

getting a average value in Perl

P: 5
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
Oct 14 '08 #1
Share this Question
Share on Google+
12 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Oct 14 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
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.
Oct 14 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
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.
Oct 14 '08 #4

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

Expand|Select|Wrap|Line Numbers
  1. open(SAROUT,"/tmp/input_file" ) or die "Can't open $!";
  2.  
  3. while(<SAROUT>) {
  4.    chomp;
  5.    s/\s+/ /g; s/^\s+//g;
  6.    $Lines++;
  7.  
  8.    if ( $FoundBegin eq 1 ) {
  9.      print "Lines in between: $_\n";
  10.      ( $Disk, $DiskBusy, $Avque, $Rws, $Blks, $DiskWaitIO, $DiskSvcTime ) = split / /, $_;
  11.      $Disks{$Lines}{'disk'}=$Disk;
  12.      $Disks{$Lines}{'busy'}=$DiskBusy;
  13.      $Disks{$Lines}{'waitio'}=$DiskWaitIO;
  14.      $Disks{$Lines}{'svctime'}=$DiskSvcTime;
  15.  
  16.    }
  17.  
  18.    if ( /$BeginTime/ )  {
  19.      $Lines=0;
  20.      print "Begin Line: $_\n";
  21.      ( $SarTime, $Disk, $DiskBusy, $Avque, $Rws, $Blks, $DiskWaitIO, $DiskSvcTime ) = split / /, $_;
  22.      $Disks{$Lines}{'disk'}=$Disk;
  23.      $Disks{$Lines}{'busy'}=$DiskBusy;
  24.      $Disks{$Lines}{'waitio'}=$DiskWaitIO;
  25.      $Disks{$Lines}{'svctime'}=$DiskSvcTime;
  26.      $FoundBegin=1;
  27.  
  28.  
  29.    } elsif ( /$EndTime/ ) {
  30.      $Entries[$Lines]=$_;
  31.      print "End Line: $Lines\n";
  32.      close(SAROUT);
  33.    }
Oct 15 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
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?
Oct 15 '08 #6

P: 5
sorry for confusion..replace the open with..The begin and End pattern is 00:10:00 and 00:20:00 for example..

Expand|Select|Wrap|Line Numbers
  1. 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

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


Thansk a lot!
Oct 15 '08 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Oct 15 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
You're making it too hard. Maybe someone else will take the time to try and figure out what you want.
Oct 15 '08 #9

P: 5
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
Oct 16 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
OK, sticking with your original sample data and assuming each field is averaged seperately:

Expand|Select|Wrap|Line Numbers
  1. #use Data::Dumper;
  2. my %HoA = ();
  3. while (<DATA>){
  4.    chomp;
  5.    my @data = split(/\s+/);
  6.    for my $i (1..3){
  7.       $HoA{$data[0]}[$i-1]+=$data[$i];
  8.    }
  9.    $HoA{$data[0]}[3]++;
  10. }
  11. #print Dumper \%HoA;
  12. foreach my $key (keys %HoA) {
  13.    print "Average for $key: ";
  14.    foreach my $i ( 0..$#{$HoA{$key}}-1 ) {
  15.       if ($HoA{$key}[$i] == 0) {
  16.          print "0 ";
  17.       }
  18.       else {
  19.          my $s = sprintf  "%.4f", $HoA{$key}[$i] / $HoA{$key}[3];
  20.          print "$s ";
  21.       }
  22.    }
  23.    print "\n";
  24. }
  25. __DATA__
  26. c42t3d0 0.01 0.00 1.19
  27. c53t2d6 2.00 3.4 1.76
  28. c12t10d4 0.01 0.00 1.42
  29. c53t2d6 0.03 0.00 0.59
  30. c12t10d4 0.02 0.00 0.82
  31.  
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
Oct 16 '08 #11

P: 5
WOW! Thank you very much Kevin! Appreciate it man..

I didn't know this technique.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. $HoA{$data[0]}[$i-1]+=$data[$i];
  4.  
  5.  
Oct 16 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
You're welcome. I hope it helps.
Oct 16 '08 #13

Post your reply

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