in reply to Loop Control : REDO

You are gonna smack yourself... you put a $1(one) instead of $i in your checks...

It works then but allows letters... maybe wrap it in an int()?

Update Wrapping it in an int makes letter entries zero, maybe instead of chomp do

#!/prod/gnu/bin/perl print "** Random 4 Lotto Numbers **\n"; print "** Type any four digits (0 to 9) **\n\n"; for ($i=1; $i <= 4; $i++) { print "Choice $i: "; ($digit[$i] = <>) =~ s/\D//g; redo unless $digit[$i]; redo if $digit[$i] > 9; redo if $digit[$i] < 0; } print "Your choices :@digit \n";
then you just get the numbers.
                - Ant

Replies are listed 'Best First'.
Re: Re: Loop Control : REDO
by skullbowl (Monk) on May 02, 2001 at 17:54 UTC
    Thanks suaveant for spotting the typo. The example came in the cd but i didn't spot that.
Re: Re: Loop Control : REDO
by iakobski (Pilgrim) on May 02, 2001 at 18:18 UTC
    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

        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
      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