I'm having difficulties compiling my script. The purpose of this script it to take numerical data from file #1 (input file), finding the largest, smallest, and the average of the said data and printing it to file #2 (output file).

I haven't attempted to go an further than the average because my script doesn't seem to recognize my data in the input file.

Input file as off right now just has the numbers 1-10. Here is my code:

#!/usr/bin/perl -w use strict; my @lines; my $ave; print "Enter an input file name: "; chomp (my $infile = <STDIN>); while (!(-e $infile)) { print "Invalid file name, Try again: "; chomp ($infile = <STDIN>); } print "\n"; print "Enter an output file name: "; chomp (my $outfile = <STDIN>); open (FIN,'<', $infile) or die "$!\n"; chomp (@lines = <FIN>); open (FOUT, '>', $outfile) or die "$!\n"; while (<STDIN>) { $ave = average(<FIN>); print FOUT $ave; } close FOUT; sub average { my $sum = 0; my $size = @lines; $sum = $sum + $_ foreach @lines; $sum / $size; } sub largest { my $max; for (@lines){ $max = $_ if !$max || $_ > $max; } $max }

I keep getting an error saying Illegal division by zero in bsad.pl at line 52. Please help me solve this mystery.


In reply to 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.