Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

As far as i understood the basic theme is: "...match a specific part of a line and print the line above and below it, in full." Please correct me if i'm wrong.

I would do it like this:

#!/usr/bin/perl use strict; use warnings; use Tie::File; # my $pattern = qr/(^4000.+)/; # my $pattern = qr/(^4001.+)/; my $pattern = qr/(^4002.+)/; # my $pattern = qr/(4003.+)/; # my $pattern = qr/(^4004.+)/; # my $pattern = qr/(^4005.+)/; tie my @lines, 'Tie::File', shift || die; my $idx = 0; for my $line(@lines){ print qq($idx $line\n); if( $line =~ m/($pattern)/ ){ if( $idx == 0){ print qq(Heuraka: $1 next: $lines[ ( $idx + 1) ]\n); }; if ( $idx == scalar( @lines - 1 ) ) { print qq(Heuraka: $1 previous: $lines[ ( $idx - 1 )] \n); }; if ( $idx ~~ [ 1..scalar( @lines - 2 ) ]) { print qq(Heuraka: $1 previous: $lines[ ( $idx - 1 ) ] next: +$lines[ ( $idx + 1 ) ]\n); }; } ++$idx; } untie @lines || die; __END__ Karls-Mac-mini:Desktop karl$ cat MyData.txt 4000_1#0 4001_1#1 4002_1#2 4003_1#3 4004_1#4 4005_1#5 Karls-Mac-mini:Desktop karl$ ./ MyData.txt 0 4000_1#0 1 4001_1#1 2 4002_1#2 Heuraka: 4002_1#2 previous: 4001_1#1 next: 4003_1#3 3 4003_1#3 4 4004_1#4 5 4005_1#5

See also: Tie::File

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

In reply to Re: How to print the lines immediately above and below a matching line? by karlgoethebier
in thread How to print the lines immediately above and below a matching line? by Bio90

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (12)
    As of 2018-03-19 17:21 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (245 votes). Check out past polls.