Hi MikeyG,
Finding the maximum value in a list is a common task. So is adding up the values. Reading from and writing to files are very common tasks. One of the reasons to use Perl is so you can get stuff done quickly: it "makes the easy stuff easy and the hard stuff possible."
So you can use standard tools for doing everyday tasks, and get back to solving your actual problem.
Output:use strict; use warnings; use Path::Tiny qw/ path /; use List::Util qw/ max sum /; my $in = '1159573.dat'; my $out = '1159573.out'; my @lines = path( $in )->lines({ chomp => 1 }); my $max = max @lines; my $avg = sum( @lines ) / scalar( @lines ); path( $out )->spew( @lines . " values; max: $max; avg: $avg" ); __END__
Note: you'll want to handle the case of no lines in your file so you don't try to divide by zero. If you're running your script periodically and you don't want to create a fresh output file each time, use path( $out )->append() instead.$ cat 1159573.out 10 values; max: 10; avg: 5.5
Note: List::Util is distributed with Perl so you have it already, and Path::Tiny is one file, pure-Perl (no compiler required) with no non-core dependencies, so you can run it no matter what.
Hope this helps!
In reply to Re: Importing Data and Exporting Data Issues
by 1nickt
in thread Importing Data and Exporting Data Issues
by MikeyG
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |