Re: Use of uninitialized value in addition
by ikegami (Patriarch) on Mar 26, 2010 at 03:48 UTC
|
If you have 5 values, $total is 5 and the values in are in $value[0] to $value[4]
for my $valNum (0 .. $total){
should be
for my $valNum (0 .. $total-1){
| [reply] [d/l] [select] |
Re: Use of uninitialized value in addition
by NetWallah (Canon) on Mar 26, 2010 at 03:55 UTC
|
After you are done with collecting values, even after "$total--"
, your $total is one higher than it should be.
This is because you increment $total before putting anything into the corresponding index of @value.
The simplistic fix would be to do an additional $total--;
But you will need to divide by $total+1 (Number of elements).
I think the main issue is that you expect $total to be both an index, and a count of number of values.
There are numerous other coding style suggestions - I'm sure others will chime in.
Update: Here is a simplistic, more perlish rewrite of your code, including some error checking.
#!/usr/bin/perl
use strict;
use warnings;
print "\nMean Average Program\n\nenter values, when done enter 'done':
+";
my @value;
while (my $input=<>){
chomp $input;
last if uc($input) eq "DONE";
#Validate that the input is only digits-should reallly [use Scalar::
+Util qw(looks_like_number)]
print ("INVALID value '$input' -ignored\nTry again:") , next
unless $input =~ /^[+-]?\d+(\.\d+)?$/; # Crude floating-point
+test
push @value, $input ;
print "Enter a value('done' if finished): ";
}
my $mean = 0;
for my $val (@value){
$mean += $val;
}
if (scalar @value){ # There is at least one @value - avoid div by 0
$mean = $mean / scalar(@value);
}
print "\nThe mean of your values is $mean\n";
Theory is when you know something, but it doesn't work. Practice is when
something works, but you don't know why it works. Programmers combine Theory
and Practice: Nothing works and they don't know why.
-Anonymous
| [reply] [d/l] |
Re: Use of uninitialized value in addition
by AnomalousMonk (Archbishop) on Mar 26, 2010 at 04:46 UTC
|
And, of course, the 'Perl way' (such as it is) is not to worry about the values in the array or their number in the first place (what is it your business?), but just to use them:
>perl -wMstrict -le
"my @values = (1, 2, 3, 4, 5);
my $average;
for my $value (@values) {
$average += $value;
}
$average /= @values;
print qq{average is $average};
"
average is 3
List::Util::sum might also be useful in place of the explicit for-loop:
>perl -wMstrict -le
"use List::Util qw(sum);
my @values = (1, 2, 3, 4, 5);
my $average = sum(@values) / @values;
print qq{average is $average};
"
average is 3
The next step might be to clean up that while-loop by only push-ing a value onto the array if it's a proper value (just get out of the loop if it isn't): no need for all those index-counting and flow-control variables.
| [reply] [d/l] [select] |
Re: Use of uninitialized value in addition
by GrandFather (Saint) on Mar 26, 2010 at 09:03 UTC
|
Perl provides a a lot of leverage for this sort of program. Because arrays are dynamic and report the number of elements they contain you seldom need a separate counter. In many places you can use statement modifiers to make short work of small loops and eliminate extra temporary variables. Almost always if you are looping over the elements of an array you don't need to index into the array. So, taking those techniques into account, and adding a missing layer of error checking, consider the following:
#!/usr/bin/perl
use strict;
use warnings;
print "Mean Average Program\n";
my @values;
push @values, $_ while (defined ($_ = nextValue ()));
my $sum;
$sum += $_ for @values;
my $mean = $sum / @values;
print "\nThe mean of your values is $mean\n";
sub nextValue {
print "Enter a value ('done' if finished): ";
while (1) {
chomp (my $value = <>);
return if $value =~ m'done'i;
return $value if $value =~ /[+-]?\d+(\.\d*)?([+-]?[eE]\d+)?/;
print "I don't recognise $value as a number or as 'done'. Try
+again: ";
}
}
given the input 2, 2, 3, done prints:
Mean Average Program
Enter a value ('done' if finished): 2
Enter a value ('done' if finished): 2
Enter a value ('done' if finished): 3
Enter a value ('done' if finished): done
The mean of your values is 2.33333333333333
Note that the repeating prompt and error checking checking code are in a sub which returns undef to terminate the data collection loop or a valid number otherwise - no bogus strings used as numbers here thank you very much.
True laziness is hard work
| [reply] [d/l] [select] |
|
|
Awsome,
Thanks for all the great info. Definatley much more streamlined code. I'm learning alot here, thank you very much all. PerlMonks are Awsome!!! Special thanks to you GrandFather, this is the second time you've clarified things in way I can grasp.
| [reply] |
Re: Use of uninitialized value in addition
by kiruthika.bkite (Scribe) on Mar 26, 2010 at 04:12 UTC
|
Before performing addition operation ,check whether that array index is having value or not using define function.
Your code,
for my $valNum (0 .. $total){
$mean += $value[$valNum];
}
Change the loop like the following,
for my $valNum (0 .. $total-1){
if(defined($value[$valNum]))
{
$mean += $value[$valNum];
}
}
| [reply] [d/l] [select] |
Re: Use of uninitialized value in addition
by Marshall (Canon) on Mar 26, 2010 at 07:35 UTC
|
Looks to me like you have an application that averages some input
numbers.
1. You of course need a command loop.
What you should strive to do with a while() command loop is to put
the thing that ends the loop within the while()..that advice actually
goes for any while() loop!
The normal way in 'C' or Perl is to use the comma operator so that
the user prompt and the ending condition is all in one single
statement.
The main command loop "while" statement below does some "heavy lifting".
The user is prompted, the input line from stdin is captured and it is
checked against a variety of things: eg: D, d, done, DoNE. If one of those
ends the loop, it is right there in the while() at the start of the
loop. When using the comma operator, the true/false value is only
dependent upon the last part of the statement.
2. A blank user line should be skipped.
3. There should be some validation of the user input and there is
a line that does that. Maybe my regex is not perfect, but it is pretty close. Adjust
this if needed.
4. Don't save stuff that is not need later. Use it now if you can.
Here we just need the mean or average, so all we need is the
total and the divisor - not the individual numbers as an array.
5. Oh, the chomp() in the errror message was needed because the
"main line" code is independent of this line ending detail.
#!/usr/bin/perl -w
use strict;
my $total = 0;
my $nums = 0;
print "Average numbers: enter numbers then \"done\"\n";
while ( (print "Enter Number: "),
(my $line=<STDIN>) !~ /^\s*d(?:one)?\s*$/i
)
{
next if $line =~ /^\s*$/; #re-prompt on blank lines
if ($line !~ /^\s*((-?\d*)(\.\d*)?)\s*$/) #valid float
{
chomp($line);
print "$line is not a valid number! Try again!\n";
next;
}
$total += $1;
$nums++;
}
print "average is : ",$total/$nums,"\n";
| [reply] [d/l] |
|
|
| [reply] |
|
|
I would agree that the comma
operator can produce absurd, nutty code like:
while (expr1, expr2, expr3, expr4, expr5, expr6, expr7)
The ONLY time I use the comma operator is in the following
way:
while ((print prompt), test-response-to-prompt)
How is this "opaque"?
My code prepends a single print statement in the "while", a very
simple thing who's return value is ignored. Without the
comma operator, I would have to prompt before the loop and then
prompt before the next loop. This can lead to many prompt statements
or calls to a prompt subroutine for each error condition instead of
just "next;".
The print statement at the beginning has no effect upon the test-response-to-prompt code. Use of the
comma operator avoids 2 or more other statements.
I avoid while(1) except in the case of servers. Those loops
never exit. I consider your while(1) with multiple returns within
the FOREVER statement (a typical #define for while (1)or for (;;)) to be
confusing. You have to read the code to figure out what the "end of loop"
condition really is.
I argue that it is better to put the "end of loop" condition in the "while" or
"for" loop, right at the top rather than "burying it" within a FOREVER loop.
| [reply] |
|
|
|
|
|
|
|
|
|