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.

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__
Output:
$ cat 1159573.out 10 values; max: 10; avg: 5.5
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.

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!


The way forward always starts with a minimal test.

In reply to Re: Importing Data and Exporting Data Issues by 1nickt
in thread Importing Data and Exporting Data Issues by MikeyG

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.