Hi, the first problem is that your two loops are the wrong way round: you want to read each line once (in the outer loop), than examine that line against each of the possible patterns (in the inner loop).
The second problem is that you are reading the lines from the file into the default variable ($_), but then processing as if it had been read into $pattern. ("Pattern" is also an unexpected name for it - in usual terminology, the pattern would be the regular expression, and we'd talk about "matching a string against a pattern".)
I'd also recommend using a different delimiter for the pattern such as {...} so that you don't need to escape the literal '/' characters in the pattern, and using a variable like $fh for the filehandle in preference to a bareword "FH".
So I'd suggest something like this:
use strict; # always
use warnings; # always
my $file = "/home/test.txt";
if (-e $file) {
my @list = ("Anls", "core", "route");
open(my $fh, '<', $file) or die "$file: $!";
while (my $line = <$fh>) {
foreach my $x (@list) {
if ($line =~ m{$x/(.*)/(.*)}) {
my $version = $1;
print "$x: $version\n";
}
}
}
close $fh;
}
Update: just after posting, it occurs to me that it is quite likely that you intend a line like "score/01.00/windows" not to match your "core" pattern. If I'm correct, then the pattern should additionally be anchored to the start of the string:
if ($line =~ m{^$x/(.*)/(.*)}) { |