Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:16:47+00:00 2026-06-14T15:16:47+00:00

#!/usr/bin/perl use strict; use warnings; use warnings; use 5.010; my @names = (RD, HD,

  • 0
#!/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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:16:48+00:00Added an answer on June 14, 2026 at 3:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#!usr/bin/perl use strict; use warnings; my $file_name = rem.txt; open(FILE, $file_name); while (<FILE>) {
#!/usr/bin/perl use strict; use warnings; open(TEST, leet.txt) or die Can't open leet.txt: $!\n; while(my
#!/usr/bin/perl use strict; use warnings; sub paragraph { open my $file, <, dict.txt or
Which version would you prefer? #!/usr/bin/env perl use warnings; use strict; use 5.010; my
#!/usr/bin/perl use strict; use warnings; open(my $vmstat, /usr/bin/vmstat 1 2>&1 |); open(my $foo, >,
with #!/usr/bin/perl use warnings; use strict; use 5.010; use SQL::Parser; my $parser = SQL::Parser->new(
I have code like: #!/usr/bin/perl use strict; use warnings; open(IO,<source.html); my $variable = do
#!/usr/local/bin/perl use strict; use warnings; system(cd $PATH /usr/bin/sftp mysite.com <<!EOF put sample.txt bye !EPF);
I have the following sample code #!/usr/bin/perl use strict; use warnings; my $b =
#!/usr/bin/perl use strict; use warnings FATAL => 'all'; use 5.010; use XML::Twig; sub get_xml_from_data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.