Your method requires 2 full passes of the file, and then creates the problem of trying to relate the results of the two.

Far simpler would be to do a single pass, remembering each section and discarding it when you see the start of a new one. And then print the remembered section when you see the search term. You have a single pass, that on average will complete half way through the file, and no silly games trying to relate two separate searches.

Ie:

#! perl -sw use strict; my $term = shift; my @section; while( <DATA> ) { @section = () if /Header/; push @section, $_; if( /$term/ ) { print for @section; print while defined( $_ = <DATA> ) and not /^Header/; last; } } __DATA__ Header 1 just some junk just some junk just some junk just some junk just some junk Header 2 just some junk just some junk just some junk just some junk just some junk Header 3 just some junk just some junk just some junk this is a term just some junk just some junk Header 4 just some junk just some junk just some junk just some junk just some junk Header 5 just some junk just some junk a different term just some junk just some junk just some junk

A couple of sample runs:

c:\test>junk34 term Header 3 just some junk just some junk just some junk this is a term just some junk just some junk c:\test>junk34 different Header 5 just some junk just some junk a different term just some junk just some junk just some junk

Note that this stops looking the first time it matches the term. To display all sections containing the term, just delete the last statement.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: search array for closest lower and higher number from another array by BrowserUk
in thread search array for closest lower and higher number from another array by bigbot

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.