×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Skip processing current pattern and continue with next

Skip processing current pattern and continue with next

Skip processing current pattern and continue with next

(OP)
I want to stop processing the action of the current pattern and continue with the next pattern.
I am familiar with "continue", "break" and "next" but none seem to do what I want.

For example:

pattern1 {
many lines, nested loops etc.

if (condition) Skip_rest_of_pattern1_and_continue_with_pattern2

many lines, nested loops etc.
}

pattern2 {
more code
}

RE: Skip processing current pattern and continue with next

When you are on pattern1 and if condition occurs you simply do nothing and skip to the next pttern2.

For example you have a file:
david_klein.txt

CODE

line 1 bar
line 2
line 3 foo bar baz
line 4
line 5 

and this script:
david_klein.awk

CODE

# awk -f david_klein.awk david_klein.txt
/bar/ {
  if ($0 ~ /baz/) {
    # skip
  } 
  else {
    print "found bar: " $0
  } 
}

/foo/ {
  print "found foo: " $0    
} 

If you are on a line which contains the pattern /bar/ and if the condition ($0 ~ /baz/) occurs you can simply do nothing and skip to the next pattern which is /foo/
Output:

CODE

$ awk -f david_klein.awk david_klein.txt
found bar: line 1 bar
found foo: line 3 foo bar baz 

RE: Skip processing current pattern and continue with next

Hi

microm, I think the OP wants to return early from a more complex condition hell.

pattern1 {
    many lines, nested loops etc.

    if (condition) {
        many lines, nested loops etc.

        if (condition) {
            many lines, nested loops etc.

            if (condition) {
                Skip_rest_of_pattern1_and_continue_with_pattern2
            }

            many lines, nested loops etc.
        } else {
            many lines, nested loops etc.
        }

        many lines, nested loops etc.
    } else {
        many lines, nested loops etc.
    }

    many lines, nested loops etc.
}

pattern2 {
    more code
}
 
Personally I would think about moving pattern1's action in a dummy function from where to be able to return in any desired moment, then call only that dummy function on pattern1. Ugly, clumsy, efficient.

Feherke.
feherke.github.io

RE: Skip processing current pattern and continue with next

feherke,
What you suggested is excellent!

RE: Skip processing current pattern and continue with next

(OP)
feherke, exactly what I was looking for. Thanks!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close