Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: seek and process from there on

by Zaxo (Archbishop)
on May 30, 2006 at 14:15 UTC ( [id://552504]=note: print w/replies, xml ) Need Help??


in reply to seek and process from there on

Another way, with the scalar range operator,

while (<$fh>) { next unless /^internal name/..0 and /^need this/; print "Got one in line ${.}\n"; }
Scalar range, once true, stays true until its right argument goes true.

Update: Fixed thinko on when the op switches off.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: seek and process from there on
by Anonymous Monk on May 30, 2006 at 14:57 UTC
    Thanks for all the answers, I just wish I knew which one was the best option !
      There are really only two ways of doing this. One is to use two loops. In the first loop, you look for "internal name", then go to your other loop to look for "need this". Becareful to account for not finding "internal name":
      open (MY_DATA, "$someFile") or die qq(aauugh!); while (<MY_DATA>) { next unless (/^internal name/); } while (<MY_DATA>) { next unless (/^need this/); <do something> }
      The above was written mainly for clarity. You could have used a for loop for the first loop which some people will argue is better because the test is in the outside of the loop instead of being buried in the loop itself.

      I also didn't test in the second loop to see if I actually found "internal name" (like I said should be done). You could argue that it isn't necessary since the second loop won't execute if the first loop doesn't find "internal name" anyway.

      However, there may be a difference between not finding "internal name" and finding "internal name" but not finding "need this". I would probably set a flag in the first loop:

      open (MY_DATA, "$someFile") or die qq(aauugh!); my $internalNameFlag = 0; while (<MY_DATA>) { next unless (/^internal name/); internalNameFlag = 1; } while ((<MY_DATA>) and ($internalNameFlag)) { next unless (/^need this/); <do something> }
      Of course, if you're setting flags, you might want to do the second solution. It uses only a single loop and uses a flag to see whether you're looking for "internal name" or "need this".
      my $internalNameFlag = 0; while (<DATA>) { if ($internalNameFlag) { if (/^need this/) { <Do something> } } else { #Internal Name Not Found Yet if (/^internal name/) { $internalNameFlag = 1; } } }
      I personally like solution #1 because I think it flows better. You're looking for "internal name". Then, when you find it, you look for "needs this".

      Others will prefer solution #2 because they like the fact that it's a single loop and not two loops. Many people feel that there should only be a single processing loop per "open".

      All of the replies to this message were really just different takes on one of these two methods. My recommendation is to do which of these two solutions makes the most sense for you, and to keep the code simple and easy for you and your coworkers to follow. Remember that someone down the line is going to have to maintain your code.

      Thanks for all the answers, I just wish I knew which one was the best option !

      I just wish I knew on which criteria you would consider a solution to be the best, and I may possibly give you more helpful an answer!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://552504]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-25 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found