Q:

how to compare the the array values with different file in different directory?

#!/usr/bin/perl
use strict;
use warnings;
use warnings;
use 5.010;

my @names = ("RD", "HD", "MP");
my $flag = 0;
my $filename = 'Sample.txt';

if (open(my $fh, '<', $filename))
{
    while (my $row = <$fh>) 
    {
        foreach my $i (0 .. $#names) 
        {
            if( scalar $row =~ / \G (.*?) ($names[$i]) /xg )
            {
                $flag=1;
            }
        }

    }

    if( $flag ==1)
    {
       say $filename;
    }

    $flag=0;
} 

here i read the content from one file and compare with array values, if file contant matches with array value i just display the file. in the same way how can i access different file from different direcory and compare the array values with same?

A:

Q: How can I access a different file?
A: By specifying a different filename.

By the way: If you are using flags for loop control in Perl, you are doing something wrong. You can specify that this was the last iteration of the loop (in C: break), or that you want to start the next iteration. You can label the loops so that you can break out of as many loops as you like at once:
#!/usr/bin/perl
use 5.010; use warnings;

my @names = qw(RD HD MP);

# unpack command line arguments
my ($filename) = @ARGV;

open my $fh, "<", $filename or die "Oh noes, $filename is bad: $!";

LINE:
while (my $line = <$fh>) {
    NAME:
    foreach my $name (@names) {
        if ($line =~ /\Q$name\E/) { # \QUOT\E the $name to escape everything
            say "$filename contains $name";
            last LINE;
        }
    }
}

Other highlights:

using a foreach loop as intended and
removing the (in this context) senseless \G assertion

You can then execute the script as perl script.pl Sample.txt or perl script.pl ../another.dir/foo.bar or whatever.

