WisDomSeeKer34 has asked for the wisdom of the Perl Monks concerning the following question:

The code says it all:
#!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number: 1 2 3 4 5 "} choose(); my $x = <STDIN>; say $x; if ($x<1 or $x>5) { say"You have chosen the wrong number"; choose(); $x = <STDIN>; }

If you enter the wrong number, you get the new message again. But only once. I want the message to repeat until you type in the right number.

I am working on this code, because I have still the Basic syntax in mind:

if ($x<1 or $x>5) THEN GOTO 10

and that doesn't work that way in Perl.

Replies are listed 'Best First'.
Re: Looking for a loop
by Athanasius (Archbishop) on May 28, 2018 at 07:54 UTC

    Hello WisDomSeeKer34, and welcome to the Monastery!

    Since you want the message to repeat (if necessary), you need a loop. In this case, changing if to while is all you need. :-)

    Update: It might be clearer to use until here rather than while — that allows you to write the condition in the form of a specification. Also, the loop and the calls to <STDIN> should be placed in the subroutine:

    sub choose { say "choose a number: 1 2 3 4 5 "; my $choice = <STDIN>; until ($choice >= 1 && $choice <= 5) { say "You have chosen the wrong number"; $choice = <STDIN>; } return $choice; }

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hello Athanasius,

      I think WisDomSeeKer34 should also add chomp(my $choice = <STDIN>); :)

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Looking for a loop
by karlgoethebier (Abbot) on May 28, 2018 at 17:40 UTC
    "...still the Basic syntax in mind..."

    If it is more comfortable for you or if you feel better, try to replace $choice = <STDIN>; with goto &choose; in the sub Athanasius provided. Untested but it should work. See also goto for some differences.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Looking for a loop
by Marshall (Canon) on May 30, 2018 at 06:30 UTC
    Another way...
    Always allow leading and trailing spaces for user input.
    !/usr/bin/perl use strict; use warnings; sub getNum { my $in; while ( (print "Enter 1..5: "), $in = <STDIN>, $in !~ /^\s*[12345]\s*$/) { print "Illegal entry, Try Again!\n"; } $in +=0; # or $in =~ s/^\s*|\s*$//g; return $in; } print getNum(),"\n"; sub getNumv2 { my @nums = @_; my $in; while ( (print "Enter @nums: "), $in = <STDIN>, $in !~ /^\s*[@nums]\s*$/ or $in =~ /^\s*$/) # no blanks { print "Illegal entry, Try Again!\n"; } $in +=0; # or $in =~ s/^\s*|\s*$//g; return $in; } print getNumv2(3,4,8,9);
Re: Looking for a loop
by Anonymous Monk on May 28, 2018 at 15:59 UTC
    Another way I often see it done (in any language):
    $correct = false; // until otherwise do { ... prompt ... if (validation check) { $correct = true; // often 'last' here to end loop } say("You have chosen the wrong number") } until ($correct)

      Translated to perl:

      #!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number:"; say for 1..5; } my ($correct, $response); while(!$correct){ choose(); chomp($response = <>); if($response >= 1 && $response <= 5){ $correct = 1; } else{ say "You have chosen the wrong number"; } }

      But why not get rid of $correct, since you can just exit the loop?

      #!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number:"; say for 1..5; } my $response; while(1){ choose(); chomp($response = <>); last if $response >= 1 && $response <= 5; say "You have chosen the wrong number"; }