By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

Replacing value of a string and adding original value to an array?

P: 1
Okay, so I want to create a loop that continuously asks for a set of three numbers, squares them, adds them together, then compares them. I'm not really done, so I apologize if you don't really get what I'm trying to do here.

Here's what I've got so far:


use warnings;

while(1==1){
print "Enter the first number: ";
$number1 = <STDIN>;
print "Enter the second number: ";
$number2 = <STDIN>;
print "Enter the third number: ";
$number3 = <STDIN>;

$total = $number3**2+$number2**2+$number1**2;
print "$total\n";

print "Are you done? Enter 1 for yes or 2 for no.\n";
$answer4 = <STDIN>;

if($answer4==1) {
last;
}
elsif ($answer4==2) {
redo;

}
}
## here I wanna compare the numbers
print "You have finished.";

I'm only a beginner at this, so I apologize if it's kinda simple.
But I want the scalar variable $total to be continuously re-evaluated for each time it loops, but add its original value to an array.

I don't really know if that's possible, but if it is, please help.
Jul 30 '10 #1
Share this Question
Share on Google+
2 Replies


Expert
P: 70
What do you mean when you say "then compares them"? Compare what to what?

It is a good idea to chomp your input.
Jul 31 '10 #2

nithinpes
Expert 100+
P: 410
I am not sure what you want "to compare" here. But if you want to create the scalar fresh with each iteration, use 'my' (for local scope). You can later push each value into an array.

Expand|Select|Wrap|Line Numbers
  1. my $total = $number3**2+$number2**2+$number1**2;
  2. print "$total\n";
  3.  
  4. push @totals,$total;
  5.  
Aug 3 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.