in reply to Grep n awk

## cat p.pl ; perl p.pl # This is "grep -f" like. use v5.10.0; use strict; my @in = ( "polka\n" , "avoid redding dot me\n" , "dot me\n" , "yellow\n" ); my $find = 'me'; my $avoid = 'avoid'; for my $ignore ( $avoid , undef ) { process_input( $find , $ignore , @in ); print "- - - -\n"; } exit; sub process_input { my ( $grep , $skip , @in ) = @_; my $show; while ( my $line = shift @in ) { $show = find( $grep , $line ) && ( defined $skip ? ! find ( $skip , $line ) : 1 ) ; } continue { $show and print $line; } return; } sub find { my ( $find , $line ) = @_; defined $find && defined $line or return; my @break = split '' , $find; my $size = scalar @break; my $start = 0; my ( @seen ); for my $c ( @break ) { my $i = index $line , $c , $start; $i == -1 || scalar @seen == $size and last; push @seen , $i; if ( scalar @seen > 1 ) { $seen[-1] - $seen[-2] == 1 or last; } $start = $i + 1; } return scalar @seen == $size } __END__ dot me - - - - avoid redding dot me dot me - - - -

Replies are listed 'Best First'.
Re^2: grep(1) like functionality in Perl
by Anonymous Monk on Jan 04, 2013 at 02:58 UTC

    I had made a grave error when I declared ...

    ... # This is "grep -f" like. ...

    ... for there was nothing of that sort. I should have just written fgrep instead of futzing with option name to indicate grep to run in fixed string search mode. To repent, comment should have been ...

    ... # This partially tested program has limited # fgrep-like behaviour. ...