in reply to Re: Loop Control : REDO
in thread Loop Control : REDO

And if you're going to test the input with a regular expression, then use that on the redo line:
print "** Random 4 Lotto Numbers **\n"; print "** Type any four digits (0 to 9) **\n"; for ($i=1; $i <= 4; $i++) { print "Choice $i: "; chomp ($digit[$i] = <STDIN>); redo if $digit[$i] !~ /^\d$/; } print "Your choices :@digit \n";

iakobski

Replies are listed 'Best First'.
Re: Re: Re: Loop Control : REDO
by merlyn (Sage) on May 02, 2001 at 18:23 UTC
      Ahh, actually no... his checks for a 0-9, your version just gets rid of non-digits... so you'd need the extra check, and no longer be simpler...
                      - Ant
Re: Re: Re: Loop Control : REDO
by suaveant (Parson) on May 02, 2001 at 18:35 UTC
    yours is good, but why chomp? no need with that regexp...
    print "** Random 4 Lotto Numbers **\n"; print "** Type any four digits (0 to 9) **\n"; for ($i=1; $i <= 4; $i++) { print "Choice $i: "; redo if ($digit[$i] = <STDIN>) !~ /^\d$/; } print "Your choices :@digit \n";
    since the $ works fine with \n there
                    - Ant
      You are quite right that $ in regex will match at the end of the line, but you still need the chomp, otherwise the newlines get into the array.

      iakobski

        Ack... yup... my bad... :)
                        - Ant