in reply to Re^2: push to an array lines matching a pattern, and the lines before and after the matching
in thread push to an array lines matching a pattern, and the lines before and after the matching

But it's hard to believe that there isn't already an full featured unix-grep emulation in pure perl, and we are reinventing the wheel.
It's called ack. And it is a reinvention of the wheel. acks usefulness doesn't come from the fact it's implemented in Perl, but because it has some features that grep doesn't have, and has some better defaults.

In the OP's case, he's better of with calling grep, then with ack. (grep is expected to be faster (as it's in C, not in Perl))

  • Comment on Re^3: push to an array lines matching a pattern, and the lines before and after the matching
  • Download Code

Replies are listed 'Best First'.
Re^4: push to an array lines matching a pattern, and the lines before and after the matching
by LanX (Saint) on Mar 03, 2012 at 03:25 UTC
    Thanks! I remember now...

    Great name BTW, easy to guess! (sarcasm)

    At least man -k grep lists it.

    Cheers Rolf

Re^4: push to an array lines matching a pattern, and the lines before and after the matching
by gianni (Novice) on Mar 04, 2012 at 10:08 UTC
    thanks for the ack suggestion, but it is slower than the "pure perl" implementation when using anchors or \b,\s etc
    sub sub6 { #perl ack my $p = $_[0]; #pattern my $mR = $_[1]; #more rows my @values; my $time = [gettimeofday]; my @valori = qx (ack -C $mR "$p" textMatchInAfile.txt) or die "system @values` failed: $?"; say 'number of values found with ack' . $#valori; say 'time sub6 ack' . tv_interval($time); }