Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Any tips on this?

by Anonymous Monk
on Feb 04, 2014 at 16:45 UTC ( #1073440=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am supposed to write a snipet of code for the following problem: The idea is to ask the user to provide a number (an initial number) and then have him providing numbers for as long as they are greater or equal to the previous ones.
I have written this:
print "Please give me a number:\n"; my $initial_number = <STDIN>; chomp $initial_number; print "Give me another number:\n"; my $new_number = <STDIN>; if($new_number>=$initial_number) { print "Please continue with a new number:"; } else { print "You specified a number which is smaller than the previous. +The program will now exit\n"; exit; }

but as you can see it only executes once...I think that I need a while loop, but I am not really sure on how to implement this. Can you point me to the correct direction?

Comment on Any tips on this?
Download Code
Re: Any tips on this?
by Eily (Hermit) on Feb 04, 2014 at 17:03 UTC

    Well you need a while loop indeed (or rather, a while loop is the obvious way to do it). Just write while instead of if, and do think about changing the value of $initial_number (maybe change its name) to give it the value of $new_number at the end of the loop. There's no else with a while loop.

    And since this is very basic perl, you may want to read perlintro

Re: Any tips on this?
by toolic (Chancellor) on Feb 04, 2014 at 17:08 UTC
    An infinite while loop will work:
    use warnings; use strict; print "Please give me a number:\n"; my $initial_number = <STDIN>; chomp $initial_number; while (1) { print "Please continue with a new number:"; my $new_number = <STDIN>; chomp $new_number; if ($new_number < $initial_number) { print "You specified a number which is smaller than the previo +us. The program will now exit\n"; exit; } $initial_number = $new_number; }
      And the last statement will exit (I believe ...) from that loop. The next statement will branch back to the start of the loop again. As long as there is eventually some way out of the loop, an infinite loop is sometimes perfect perfect perfect perfect perfect perfect perfect perfect perfect perfect.
        Ah, thanks to all of you for your suggestions!
Re: Any tips on this?
by simmisam (Novice) on Feb 05, 2014 at 07:02 UTC
    print "Enter num1\n"; my $num1 = <STDIN>; chomp ($num1); print "Enter num2\n"; my $num2 = <STDIN>; chomp($num2); while($num2 > $num1) { print "Enter num again\n"; my $num = <STDIN>; chomp($num); $num1 = $num2; $num2 = $num; } print "Num2 is smaller than Num1\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1073440]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-09-23 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls