in reply to Loop Control : REDO

Although I imagine that what you're reading is probably just trying to explain loops in perl, that's probably not the best way to do things...
use strict; print "** Random 4 Lotto Numbers **\n"; print "** Type any four digits (0 to 9) **\n"; my @digit; foreach my $i (1..4){ while ($digit[$i] =~/^[\D]$/){ print "enter digit $i : "; chomp($digit[$i] =<>)}; } print "Your choices : @digit \n";
As an aside, I thought that redo and friends were only applicable inside of while or foreach loops, but apparently, that's not the case. Hm!

Replies are listed 'Best First'.
Re: (boo)Re: Loop Control : REDO
by iakobski (Pilgrim) on May 02, 2001 at 18:50 UTC
    It is good to point out that use strict is needed here.

    If you also add use warnings, you will notice that the print statement is accessing an undefined value,  $digit[0] I suspect that the original author meant to say the equivalent of

    foreach my $i ( 0..3){
    or if not the print should be:
    print "Your choices : @digit[1..4] \n";

    iakobski

Re: (boo)Re: Loop Control : REDO
by suaveant (Parson) on May 02, 2001 at 18:28 UTC
    From Programming Perl, 3rd Edition Page 118
    The foreach keyword is just a synonym for the for keyword, so you + can use foreach and for interchangeably...

                    - Ant
      well, YES, that is true.
      however, from perl's documentation on continue :
      If there is a continue BLOCK attached to a BLOCK (typically in a while or foreach)...

      which made me believe you could tack continues on to any block all willy nilly. This isn't the case, though.
        Yeah... I think they just use foreach as a readability thing... but they are the exact same... I haven't used foreach since I saw that... so much extra typing :)
                        - Ant