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

In reply to Re: Use of uninitialized value in addition by Marshall
in thread Use of uninitialized value in addition by Solarplight

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.