[EMAIL PROTECTED] wrote:

I've had to change the code as due to the earlier version of perl I am
running the open line would not work.

I found a workaround on the internet

use strict;
use warnings;



my $file = shift @ARGV || 'cpuuse.out';


my %fh = ();
if (-f $file  and ! $fh{$file} ) {
    open($fh{$file}, "<$file") || DisplayError();
   # coolness ensues...
}


my @cpu;

while (<$fh>) {
   my @data = split;
   push @cpu, $data[4];
}

print sum(@cpu) / @cpu;

sub sum {
   my $sum;
   $sum += $_ foreach @_;
   return $sum;
}



.... however I am getting zero values back and it is erroring
obviously about dividing by zero.

My input file looks like this.....

[snip]

This is the last part of my script I can't get working. Once this is
sorted you may never hear from me again, until the next script I
write :)

This is why it's important to understand code you're borrowing instead
of just copying it and hoping. The snippet you've copied is part of a
scheme to cache multiple file handles and reuse them if a file has
already been opened. Since you're opening only a single file this is
unnecessary and confusing for you. It's also important to take notice of
any error or warning messages that Perl is producing as John has already
pointed out.

If the Perl you're using will accept this open statement

 open($fh{$file}, "<$file") || DisplayError();

then all you need to do is replace the three-parameter open in my
previous code

  open my $fh, '<', $file or die $!;

with

  open my $fh, $file or die $!;

and all should be well. Please try this and come back to this list to
say whether you have been successful.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to