Re: Debug some simple code

2002-01-31 Thread Rambog

Thank you all for you prompt and accurate help.  I will not soon forget the
difference between assigning a value and a numeric comparison operator.

Jason, with the "@a=$number" statement, I was trying to create the list by
changing contexts.  In other words, if  returns the input in a scaler
context and "@a=" places each line in a list context (thereby
changing the context), I thought "@=$number" would work.  I was wrong.  The
"push" works great.  I have much to learn about contexts within PERL.

"Rambog" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> 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) {
> print "Please input your number:\n";
> chomp($number=);
> @a=$number;
> }
> foreach $i (@a) {
> $c+=$i;
> }
> if ($number=999) {
> print "Your total is: $c\n"
> }
>
> The program immediately terminates with and the line "Your total is:" is
> output with a  a blank value.  It never even prompts me to input a number.
> What am I doing wrong?
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Debug some simple code

2002-01-31 Thread Jeff 'japhy' Pinyan

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=);
>@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 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Debug some simple code

2002-01-31 Thread John Edwards

Change

until ($number=999) {

and 

if ($number=999) {

to == 999. You are assigning the value 999 to the $number var in both cases,
not checking if it is equal to 999.

Simple mistake, we've all made it ;)

HTH

John

P.S Here is how I would code this script.

use strict;
my ($number, $total);

until ($number == 999) {
print "Please input your number: ";
chomp($number=);
$total += $number unless $number == 999;
}

print "Your total is: $total\n"

-Original Message-
From: Rambog [mailto:[EMAIL PROTECTED]]
Sent: 31 January 2002 16:10
To: [EMAIL PROTECTED]
Subject: Debug some simple code


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) {
print "Please input your number:\n";
chomp($number=);
@a=$number;
}
foreach $i (@a) {
$c+=$i;
}
if ($number=999) {
print "Your total is: $c\n"
}

The program immediately terminates with and the line "Your total is:" is
output with a  a blank value.  It never even prompts me to input a number.
What am I doing wrong?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--Confidentiality--.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Debug some simple code

2002-01-31 Thread Hanson, Robert

You are using the assignment operator in your "until" and "if" blocks, and
*not* the comparison operator.  You need to use the double equals for what
you want...

until ($number == 999)

And

if ($number == 999)

Rob


-Original Message-
From: Rambog [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 31, 2002 11:10 AM
To: [EMAIL PROTECTED]
Subject: Debug some simple code


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) {
print "Please input your number:\n";
chomp($number=);
@a=$number;
}
foreach $i (@a) {
$c+=$i;
}
if ($number=999) {
print "Your total is: $c\n"
}

The program immediately terminates with and the line "Your total is:" is
output with a  a blank value.  It never even prompts me to input a number.
What am I doing wrong?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Debug some simple code

2002-01-31 Thread Roger Morris

At 11:09 AM 1/31/2002 -0500, you wrote:
>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) {
>print "Please input your number:\n";
>chomp($number=);
>@a=$number;
>}
>foreach $i (@a) {
>$c+=$i;
>}
>if ($number=999) {
>print "Your total is: $c\n"
>}
>
>The program immediately terminates with and the line "Your total is:" is
>output with a  a blank value.  It never even prompts me to input a number.
>What am I doing wrong?
=  is an assignment operator.  Equality is   ==

Roger
--
Roger Morris
User Services Specialist II
[EMAIL PROTECTED]
541-687-3579

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Debug some simple code

2002-01-31 Thread Jason Purdy

A couple of things here ...

1) Watch out for = vs. == ... very tricky!  Your first until loop won't even 
eval b/c $number=999 is always true.

2) @a=$number; is not what you want... I'm not even sure what that does.  You 
probably want to use the push function, like so:
push @a, $number unless $number==999;

I would also encourage you to add "-w" to your shebang line (which would have 
warned you against == vs. =) as well as 'use strict;'.

A few tweaks of your code and I think it's working like you want ... have fun 
playing around with it - you should be able to simplify this program even 
more, once you understand all the different lines and what they do. :)

until ($number==999) {
print "Please input your number:\n";
chomp($number=);
#@a=$number;
push @a, $number unless $number==999;
}
foreach $i (@a) {
$c+=$i;
}
if ($number=999) {
print "Your total is: $c\n"
}

__END__

Jason

If memory serves me right, on Thursday 31 January 2002 11:09, Rambog wrote:
> 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) {
> print "Please input your number:\n";
> chomp($number=);
> @a=$number;
> }
> foreach $i (@a) {
> $c+=$i;
> }
> if ($number=999) {
> print "Your total is: $c\n"
> }
>
> The program immediately terminates with and the line "Your total is:" is
> output with a  a blank value.  It never even prompts me to input a number.
> What am I doing wrong?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]