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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • John Ti
    New Member
    • Jul 2010
    • 1

    #1

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

    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+$nu mber2**2+$numbe r1**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.
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    What do you mean when you say "then compares them"? Compare what to what?

    It is a good idea to chomp your input.

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      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.

      Code:
      my $total = $number3**2+$number2**2+$number1**2;
      print "$total\n";
      
      push @totals,$total;

      Comment

      Working...