Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

do-until: Can't "next" outside a loop block.

by humble (Acolyte)
on Nov 26, 2012 at 06:05 UTC ( [id://1005567]=perlquestion: print w/replies, xml ) Need Help??

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

Good time! Not that it is my first script in PERL, yet I can not understand why I have such a problem w/ following simple cycle:
do{ $index_cur=int( rand( ( $last_abc_letter ) ) ); if( $index_cur==$index_prev ){ next; }else{ $index_prev=$index_cur; print "$letter[$index_cur]\n"; $key=&ReadKey( 0 ); } }until $key eq 'q';
But at run time after several successful cycles I get (when $index_cur==$index_prev): Can't "next" outside a loop block... So, it seems that next supposes it stands out of do-until. Why is so? How I can fix the issue?

Replies are listed 'Best First'.
Re: do-until: Can't "next" outside a loop block.
by 2teez (Vicar) on Nov 26, 2012 at 06:10 UTC

    Hi humble,

    if you check do documentation, you will notice this:
    "do BLOCK" does *not* count as a loop, so the loop control statements "next", "last", or "redo" cannot be used to leave or restart the block.
    check this perlsyn for alternative strategies.

    Update:
    However, If you must use the do as stated in your code with next, then you have to "double" the braces like so:

    do{{ next .... }} until ...;
    Please see perlsyn for more.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Can not to say anything but WOW! Thank You very much, 2teez!
Re: do-until: Can't "next" outside a loop block.
by marquezc329 (Scribe) on Nov 26, 2012 at 06:47 UTC

    instead of do why not try something like:

    untested

    while ($key ne "q") { $index_cur = int( rand($last_abc_letter) ); unless ($index_cur == $index_prev) { $index_prev = $index_cur; print "$letter[$index_cur]\n"; $key = ReadKey(0); } }

    I suggest reading perlintro if you haven't already. Also, if you're not already...

    use strict; use warnings;

      Thank You, marquezc329. Of course I could - but I have found the misbehave as I supposed and therefore gave my question. PS I think PERL's manuals are very cramped. - Written for the people who are the language developers rather than its users. - IMO.

        I haven't contributed much at all to the Perl development; I think maybe a POD patch or two, and several bug reports over the years. I'm not a developer of Perl. But as a user, I do consider Perl's POD to be Perl's second "killer app", right behind the CPAN.

        The POD may be dense at times. perlintro really shouldn't be, if you've got experience programming in some language. Like with most things, comprehending the documentation is one of those things that gets easier the more you work with it.


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1005567]
Approved by frozenwithjoy
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found