In the original code you posted, whenever you are done with a file, you get to the next, thanks to the foreach loop. In this situation, if you exit the while loop with a last statement, you will also get to process next file.

The new code you posted is different, there is no longer a foreach loop going though each file of the list. You can't expect it to pick up next file.

Hi Laurent! Thanks for looking in to it man. Sorry, I posted just a snippet of the same code. I actually want the code to go to the next file if my Match3 is not found in a log. Here is "almost" the same code: #!/use/bin/perl -w use strict; use warnings ;

this is a Perl one-liner that prints the first few lines of each *.pl file in my current directory. It lists all the *.pl files, opens each file and prints the content until it finds either the "my" or the use" keyword. When it find one of these words, it simply goes to the next file.

The next function with a label is certainly a possible option which will work fine, but in the case in point, I think the last function to exit the while loop and get back into the foreach loop is sufficient and in my humble view algorithmically better constructed. It also enables to cleanly close the current file at the bottom of the foeach loop before opening the next one.

Hi Lauren! I Just tried your code, it still does not skip a log that does not have ?Match3/ bu thank you for trying to help me.

That's not what I understood from your original code. Your original code was trying to break out of a loop if it found a line that matched or did not match a specified pattern. So we assumed that's what you wanted to do, and it now appears you are trying to do something else.

If you want to skip a file that does not have '?Match3/' or whatever other pattern anywhere in the whole file, then this is something entirely different and your algorithm is just plain wrong. You cannot assess whether your log file does not have some particular pattern anywhere until you've read every single line of the file until its very end. So you cannot use next or last to break out of the while loop, because you can't decide what to do before you've read everything.

I hope the difference is clear in your mind.

So you probably need to have a flag set up to false at the beginning of your file. Set it to true if you encounter somewhere the pattern you are looking for. After you've completed reading the file, take the appropriate action depending on whether the flag is still false or has been set to true.

I cannot give you a more detailed approach because you haven't really explained exactly what you are trying to do. Please explain more clearly what you are trying to achieve and provide an example of your input log files.

Hi Laurent! You understand is correct, that is exactly what I want to do. ”If you want to skip a file that does not have '/Match3/' or whatever other pattern anywhere in the whole file,…” And I’m really puzzled how to do this. My plan was like this: 1. Filter all my logs for “new” logs (if ( -M "$dir/$file" < 1 ) ) 2. Then filter “new” logs for logs that have /Match3/ and scan these logs for 3 matches /Match1 and Match2 and Match3/ and print all 3 matches out. I thought I could do this using “foreach” and “while” loops and apparently I cannot do that.

I’m sorry I was not clear on what I need to do man, it is really upsetting for me I thought I could do this by myself and I CANNOT! Thanks, testerV

I thought I could do this using “foreach” and “while” loops and apparently I cannot do that.

Yes, you can do this using “foreach” and “while” loops, but you need to read the whole file before you draw any conclusion, what you can't do is to break out of the loop early, you have to scan each file until its very end.