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

I'm receiving this error message when I come across this code bit of code:
sub NEXTKEY{ my $self = shift; my $lastkey = shift; $key = $KEYS[$self->{INDEX}++]; next if($key eq $self->{FIRSTKEY}); return($key); }
I don't know why.

Added code tags - dvergin 2002-11-23

Replies are listed 'Best First'.
(bbfu) Re: Can't "next" outside a loop block
by bbfu (Curate) on Nov 23, 2002 at 20:50 UTC

    next does not a loop make.

    The error is pretty clear, I think. You don't have a loop, so you can't next. You need to create a loop, like below. Note that you didn't have any checking for odd conditions, such as $key and $self->{FIRSTKEY} never matching. Thus, you could have run off the end of @KEYS (which I assume you define elsewhere), creating an infinite loop and lots of warnings about undefined values.

    sub NEXTKEY { my $self = shift; #my $lastkey = shift; # Do you need this? If not, don't bother shi +fting it until($self->{FIRSTKEY} eq $KEYS[$self->{INDEX}]) { ++$self->{INDEX}; last if $self->{INDEX} >= $#KEYS; # Quit if we run out of @KEYS } return($KEYS[$self->{INDEX}]); }

    PS, you should probably wrap any code you post here in <CODE> </CODE> tags, so that any special characters, like <, >, [, and ] are properly escaped.

    bbfu
    Black flowers blossum
    Fearless on my breath

Re: Can't "next" outside a loop block
by gjb (Vicar) on Nov 23, 2002 at 20:49 UTC

    next</next> in an iteration statement (<code>foreach, for or while) means that all further statements in the body of the loop are skipped and that the next iteration is started.

    In the code snippet above there's no loop statement that I can see, so next doesn't make any sense semantically, and that's exactly what the interpreter is telling you.

    Hope this helps, -gjb-

Re: Can't "next" outside a loop block
by tadman (Prior) on Nov 23, 2002 at 22:09 UTC
    I think what you intend is this:
    sub NEXTKEY { my ($self, $lastkey) = @_; # $lastkey isn't used? while (my $key = $KEYS[$self->{INDEX}++]) { if ($key ne $self->{FIRSTKEY}) { return $key; } } }
    You need a loop structure to loop, although redo can be teased to do some odd things.
Re: Can't "next" outside a loop block
by theorbtwo (Prior) on Nov 23, 2002 at 22:32 UTC

    Like everybody else said, there are better ways to do this. I'd just like to note that if you, for some reason, don't want to use any of the normal looping constructs, you can use a do{} with no while() clause:

    sub NEXTKEY { my $self=shift; my $lastkey=shift; do { $key=$KEYS[$self->{INDEX}++]; next if ($key eq $self->{FIRSTKEY}); return($key); } }


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).