MikeyG has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Importing Data and Exporting Data Issues
by GrandFather (Saint) on Apr 05, 2016 at 05:04 UTC | |
by MikeyG (Novice) on Apr 05, 2016 at 05:32 UTC | |
by Marshall (Canon) on Apr 05, 2016 at 06:55 UTC | |
by GrandFather (Saint) on Apr 05, 2016 at 12:02 UTC | |
|
Re: Importing Data and Exporting Data Issues (context)
by Athanasius (Archbishop) on Apr 05, 2016 at 06:23 UTC | |
|
Re: Importing Data and Exporting Data Issues
by 1nickt (Canon) on Apr 05, 2016 at 06:40 UTC | |
|
Re: Importing Data and Exporting Data Issues
by Anonymous Monk on Apr 05, 2016 at 04:54 UTC | |
by MikeyG (Novice) on Apr 05, 2016 at 05:06 UTC | |
by Anonymous Monk on Apr 05, 2016 at 08:07 UTC |