Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Let's call the first file your search keys, and the second file, the values mapped to those search keys. Let's also assume that the mapping file is larger and more expensive to work with than the simple search key file.

First, open and read your search key file into a hash, stripping away the > and newline characters (You might just capture m/^>(\w+:\w)/, for example). Each search key becomes a hash key. Go ahead and close your search key file but keep that hash.

Second, open the mapping file for input, and an output file. For your mapping file set the input record separator (search perlvar for $/ for an explanation) to >, so that you're only dealing with complete records, and have no need of worrying about newlines.

Now iterate over each record in the mapping file. chomp (removing the trailing >). Discard records that are empty (this takes care of the first >, for example). Then match m/^(\w+:\w)\n/, and use exists to check whether that key exists in your hash of search keys.

If you've got a match, print to your output file the current record prepended with a '>'.

That's one way to do it. When you get stuck in the actual code let us know which part is presenting difficulty. Filling in the rest of the blanks shouldn't be too much different from the solutions you obtained in some of your previous questions.


Dave


In reply to Re: print lines between a delimiter by davido
in thread print lines between a delimiter by anasuya

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 surveying the Monastery: (4)
As of 2024-04-24 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found