Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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$ ./test.pl 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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2014-10-26 01:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (149 votes), past polls