Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re^3: seek and process from there on by qazwart
in thread seek and process from there on by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found