use 5.14.0; #including features 'say' and smart match (~~)
use warnings;
my @lines = (); #lines that occurred
while (<DATA>){
my $line = /^(Line\s*+\d+\b)/ ? $1 : ''; #\b to avoid '2' matches '22'
$line ~~ @lines and do {say "$line occurred before."; next;};
push @lines, $line;
}
__DATA__
Line 1 is my favourite line
Line 2 is bad
Line 3 is bad
Line 1 is still my favourite line
Line 22 is bad
Output:
Smartmatch is experimental at ./tst.pl line 16.
Line 1 occurred before.
Whilst the above code is good to pick up line numbers that have occurred, it doesn't tell you what those lines are. If you do want that feature, then try the following code:
my @lines = ();
while (<DATA>){
my $line = /^(Line\s*+\d+\b)/ ? $1 : ''; #\b to avoid '2' matches '22'
next unless $line;
push @lines, $_;
my @occurred = grep {/$line/} @lines;
@occurred > 1 and print for @occurred;
}
Output:
Line 1 is my favourite line
Line 1 is still my favourite line