Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: get n lines before or after a pattern

by xiaoyafeng (Chaplain)
on Jul 25, 2012 at 17:48 UTC ( #983707=note: print w/replies, xml ) Need Help??

in reply to get n lines before or after a pattern

try natatime in List::MoreUtils, maybe it makes your code more elegant? ;)
use List::MoreUtils qw/natatime/; my @contents = <DATA>; pop @contents; shift @contents; my $it = natatime 8, @contents; while (my @vals = $it->()) { print "@vals[0,1,2] \n" if $vals[2] =~ /jack/; } __DATA__ start id 10 address Richmond name jack xxxxx aaaaa lastname black yyyy zzzzz id 11 address Central name rick cccccc dddddd lastname hanna eeeee yyyyy id 12 address denver name jack sssss tttttt lastname strong rrrrr mmmmm id 13 address Virginia name mick aaaaaaa ooooooo lastname jagger gggggg hhhhhh id 14 address Maine name rick sssss sssss lastname stewart ssssss ffffff end
The another advantage of this approach compared to other way is you won't lose the rest part of every chunk. you can print any elements of @vals by changing slice.

I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re^2: get n lines before or after a pattern
by Kenosis (Priest) on Jul 25, 2012 at 19:06 UTC

    Nice use of List::MoreUtils qw/natatime/! However, consider using /\bjack\b/, as your current regex also matches "jackson", "jackie", "jacklyn", etc.

Re^2: get n lines before or after a pattern
by ww (Archbishop) on Jul 25, 2012 at 20:59 UTC
    Nice (and + +), but the regex can go astray:
    C:>perl -E "my $word="jackhammer"; if ($word =~ /\bjack\b/) { say $word; } else { say \"No word-boundry-delimited 'jack's' found in $word\"; }" No word-boundry-delimited 'jack's' found in jackhammer

      Perhaps I'm missing something, but I wouldn't want to find "jackhammer" if I were searching for "jack" as the first name--as listed in the OP's data set. However, the non-word-boundary regex is perfect for finding all first names containing the sub-string "jack", as $vals[2] =~ /jack/ would.

        Flip that coin. It's not a question of searching for "jackhammer." It's a matter of what *jack* elements could be present in the data (whose contents are, at least possibly, unknown at the time you start writing the code)... as, for example, jackson (mississippi) or jackson hole, colorado.

        But note: my reply is directed, not to you, but to the parent of your node... and, thus, casts no aspersions on your prior remark; in fact, it appears to say pretty much the same thing, but you said it first

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983707]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-02-18 20:29 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.