Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the code exited out of a loop due to a last command?

Tags:

loops

perl

In a loop, I have several conditions and if the condition is not satisfied, I exit out of the loop using last syntax.

while (condition) {
    if (possibility 1) {
        ...
        last;
    }

    if (possibility 2) {
        if (possibility 3) {
            ...
            last;
        }
        ...
        last;
    }
}

After I come out of the loop, I need to know whether I used last to exit, or the condition just did not hold true any more. One way of doing that is to write a if statement that negates all the possibilities.

I am looking for some easier/more elegant way to do this.

Is there some Perl variable that stores the information that we exited out of the loop due to last? Or do I need to maintain such a variable myself?

like image 738
Lazer Avatar asked Dec 06 '25 04:12

Lazer


1 Answers

You pretty much just need to set a flag before you exit

like image 136
deek0146 Avatar answered Dec 08 '25 18:12

deek0146