#!/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?
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
lastiteration of the loop (in C: break), or that you want to start thenextiteration. You can label the loops so that you can break out of as many loops as you like at once:Other highlights:
foreachloop as intended and\GassertionYou can then execute the script as
perl script.pl Sample.txtorperl script.pl ../another.dir/foo.baror whatever.