On Jan 31, Rambog said:
>I am attempting a program that reads a list of numbers from the screen until
>the number 999 is read. It then prints the sum of all numbers read- with
>the exception of the 999.
>
>My code looks like:
>
>until ($number=999) {
You want ==, not = here.
>print "Please input your number:\n";
>chomp($number=<STDIN>);
>@a=$number;
That makes @a contain ONLY the element $number. I think you want
push @a, $number;
>}
>foreach $i (@a) {
>$c+=$i;
>}
>if ($number=999) {
Again, you want == instead of =. Furthermore, you don't even NEED the if
statement, since the only way to get out of the loop is for $number to
have equalled 999.
Another bug is that this program does not remove the 999 sentinel value
from the data. It should. I leave that up to you.
>print "Your total is: $c\n"
>}
Furthermore, you don't need an array. Why not add the numbers as you get
them?
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]