use strict; use warnings; use List::Util qw{first}; my $file = < 0) { print "$newarray[$#newarray]\n"; } else { print "-1\n"; } sub search_phrase { my ($inFile, @phrases) = @_; my @array; my $lastPhrase = $phrases[-1]; my @lines = <$inFile>; foreach my $phrase (@phrases) { # Find the first match in the remaining lines for the current phrase my $lineNo = first { $lines[$_] =~ /\Q$phrase\E/ } 0 .. $#lines; next unless defined $lineNo; # No line containing current phrase print "Found: $lines[$lineNo]" if ($lines[$lineNo] =~ m{\Q$lastPhrase\E\s*(\d+)}); push (@array, $1); $lineNo++; splice @lines, 0, $lineNo; # Delete processed lines } return @array; }