drose2211 has asked for the wisdom of the Perl Monks concerning the following question:
I am attempting to find the average of an array with this
#usr/bin/perl use strict; use warnings; use List::Util qw(sum); open IN, '<',"part4.csv" or die "can't open input file 'part4.csv': $! +\n"; my $x; my @windsordigits; foreach $x (<IN>){ @windsordigits = ($x =~ /WINDSOR\sRIVERSIDE.*(\d\d?)/); print "@windsordigits\n"; } my $average = sum(@windsordigits) / @windsordigits; print "$average"; close IN;
The issue I am having is that I am getting the "Illegal division by zero line 17, <IN> line 41. The regex only matches line 2-14. I am guessing this issue is occurring because the regex is reading all the way to line 41. Another issue I am having is that when I print out the values it is only printing the first digit and not the second. Am I on the right track here as far as the first issue? And if so is there a way to use the regex on a range of lines so that it doesn't run all the way to line 41. Here is a line from the file I am running it on.
CA006139520,"WINDSOR RIVERSIDE, ON CA",2018-01-02,10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Illegal division by zero
by AnomalousMonk (Archbishop) on Jan 24, 2018 at 05:23 UTC | |
|
Re: Illegal division by zero
by haukex (Archbishop) on Jan 24, 2018 at 08:56 UTC | |
by Random_Walk (Prior) on Jan 24, 2018 at 15:41 UTC | |
|
Re: Illegal division by zero
by Random_Walk (Prior) on Jan 24, 2018 at 09:01 UTC | |
by drose2211 (Sexton) on Jan 24, 2018 at 17:27 UTC | |
by roboticus (Chancellor) on Jan 24, 2018 at 18:28 UTC | |
by AnomalousMonk (Archbishop) on Jan 24, 2018 at 19:57 UTC |