Hello suno, and welcome to the Monastery!
As other monks have said, your question isn’t clear. The following is a guess at what you wanted:
#! perl use strict; use warnings; my @lines = ("First line\n", "Second line\n", "Third line\n", "Fourth line\n", "Fifth line\n",); my @keywords = qw(Second Fourth); my @matches; for (my $i = 0; $i < $#lines; ++$i) { my ($first_word) = $lines[$i] =~ /^\s*(\w+)\b/; for (@keywords) { if ($_ eq $first_word) # Look for a keyword { chomp(my $line1 = $lines[$i ]); # *see below chomp(my $line2 = $lines[$i + 1]); # *see below push @matches, $line1 . $line2; last; } } } my $count; print 'Match ', ++$count, ": '$_'\n" foreach @matches;
Output:
Match 1: 'Second lineThird line' Match 2: 'Fourth lineFifth line'
The key lines of code are the ones marked *see below. By applying chomp to a newly-created variable, the code removes the newline (if any) from the end of a copy of each line of text, without changing the original lines in the array.
HTH,
Athanasius <°(((>< contra mundum
In reply to Re: chomp question
by Athanasius
in thread chomp question
by suno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |