in reply to capture multiple lines

Use grep to get all matches into a new array:
@matches = grep { /keyword/ } @file_records; foreach (@matches) { push(@chomped, chomp($_)); }

Jack

Replies are listed 'Best First'.
Re^2: capture multiple lines
by johngg (Canon) on Apr 21, 2009 at 21:10 UTC
    foreach (@matches) { push(@chomped, chomp($_)); }

    Unfortunately, that's not going to do what you want. chomp returns the number of characters removed from all of its arguments, in this case, if on *nix, 1 each time for a newline. Thus, your @chomped will just contain a series of '1's. The following shows what happens with your method and also a couple of other ways to do it. (Note the -l command-line switch to append a newline after each print operation and the $count variable in the third snippet to show what chomp returns.)

    $ cat alpha abc def ghi $ perl -le ' -> @arr = <>; -> push @chomped, chomp( $_ ) for @arr; -> print for @chomped;' alpha 1 1 1 $ perl -le ' -> @arr = <>; -> @chomped = map { chomp; $_ } @arr; -> print for @chomped;' alpha abc def ghi $ perl -le ' -> @arr = <>; -> $count = chomp @arr; -> print $count; -> print for @arr;' alpha 3 abc def ghi $

    I hope this is of interest.

    Cheers,

    JohnGG