in reply to Use of uninitialized value in addition

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";

Replies are listed 'Best First'.
Re^2: Use of uninitialized value in addition
by GrandFather (Saint) on Mar 26, 2010 at 09:09 UTC

    Actually using the comma operator is generally frowned on in both C/C++ and Perl. As your sample somewhat demonstrates it leads to fairly opaque code and can produce some very subtle bugs. A better technique is to put the several lines into a sub and call that in the while loop expression. See Re: Use of uninitialized value in addition for an example.


    True laziness is hard work
      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.

        It is opaque for two reasons:

        1/ because the comma operator is seldom used so little understood. I don't mean by you (or me even - I have occasionally used it in the past), but in general. (I know this because of the complaints I've received from co-workers when I've used them - "Are you allowed to do that!".)

        2/ because the comma as operator is hard to see compared to how important its correct interpretation is to understanding the code. Most often commas are used in lists and tend to be treated as something the parser needs to do its job, but are essentially ignored by people reading the code - white space and layout are generally much better cues for understanding the form of the code.

        Regardless of how a loop is structured you have to read the code to see how it terminates. I'd rather use a C/C++ do {...} while (...); construct than the while (1) {...} loop for this sort of problem where appropriate, but Perl doesn't provide that directly. In fact it's not appropriate in the code being discussed anyhow due to the print at the end of the loop.

        Putting the termination test at the top of a loop is fine if you can do it. Where I used while (1) you can't provide a termination test because the information doesn't exist yet. However the exit conditions for the sub (and thus the loop) are very easy to see and the while (1) loop construct provides exactly the right signal - do it forever until something within the loop causes it to stop (disregarding someone putting an axe through the computer or other such event of course).

        Using the comma operator to avoid a few statements is not a good argument unless it really does make the code clearer. In practise I don't recall a single case where using the comma operator made code clearer. I do however remember some nasty cases where it made the code less clear and introduced really hard to find bugs.


        True laziness is hard work

        How is this "opaque"?

        Information overload. Input, output, assignment and (negated) regex match all crammed where a simple condition is expected. And you don't even handle EOF cleanly!