473,473 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getting a average value in Perl

5 New Member
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
12 4233
numberwhun
3,509 Recognized Expert Moderator Specialist
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
3,652 Recognized Expert Specialist
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
4,059 Recognized Expert Specialist
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
jpjacquez
5 New Member
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
4,059 Recognized Expert Specialist
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
jpjacquez
5 New Member
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
3,509 Recognized Expert Moderator Specialist
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
4,059 Recognized Expert Specialist
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
jpjacquez
5 New Member
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
4,059 Recognized Expert Specialist
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
jpjacquez
5 New Member
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
4,059 Recognized Expert Specialist
You're welcome. I hope it helps.
Oct 16 '08 #13

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

Similar topics

1
by: Mavis | last post by:
Hi, I'm trying to use a simple form to select by which column I will display output from a MySQL database. I'd like to display based on ORDER BY and select Written, Average, etc. I know the...
6
by: Aaron | last post by:
I'm trying to get an average of a long and a std::vector::size_type, but keep getting 0 for some reason. I've tried the following: float avg = some_long / some_vec.size(); float avg =...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
3
by: patelxxx | last post by:
PERL Code to Ping a server: #!c:/Perl/bin/perl.exe use Net::Ping; use strict; use warnings; my $host = 192.168.0.1; # Real value removed by MODERATOR
8
by: jonniethecodeprince | last post by:
Hello all. I need to get a html page to work with a PERL script <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"...
12
by: miguelguerin | last post by:
Im having trouble getting the input from one file to another. Main file: package assigment3; /** * * @author mguer017 */ public class Main {
6
by: kumarboston | last post by:
Hi All, I am trying to get an average value for my data, here is my data file DATA FILE EP1934.PDB 250 250 11.27 EP1934.PDB 251 251 12.7332 EP1934.PDB 252 252 6.38341 EP1934.PDB 253 253...
5
by: kumarboston | last post by:
Hi all, I was trying to calculate the average value from different parts of the same data file. For example, if suppose we have number 1 - 10 and i was trying to calculate the average of only first...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.