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

  • SEARCH
  • Home
  • 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 8041471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:13:56+00:00 2026-06-05T04:13:56+00:00

Given the files: A. hash.pl: %h1 = ( A=>[‘4631 4576′,’6646 6646’,], B=>[‘3539 4576’,], );

  • 0

Given the files:

A. hash.pl:

%h1 = (
  A=>['4631 4576','6646 6646',],
  B=>['3539 4576',],
);

B. input.txt

4576    4631    4
4576    3539    4

I have to write a Perl code that finds the values (4631 4576) in input.txt. (The order is not important.) Here, ‘4631 4576’ appears as 4576 4631 in input.txt.

I wrote the following code but there seems to be some problem:

#!/usr/bin/perl -w
open (FH, "input.txt") or die "can't open file: &! \n";
require "hash.pl";
foreach $amp (<FH>)
{
    if ($amp=~/(\d+)\t(\d+)\t(\d+)/)        
    {       
        foreach $keys (keys %h1)
        {
            @tmparray= @{$h1{$keys}};
            foreach $tmp1 (@tmparray)
            {
                if ($tmp1 =~ m/($1 $2|$2 $1)/ )
                {
                    print "$keys", "$3\n";
                }
            }
        }
    }
}
close (FH);
exit;

What is wrong with this code?

  • 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-05T04:13:58+00:00Added an answer on June 5, 2026 at 4:13 am

    This solution uses do in preference to require as the latter is intended for inluding library source files and returns a useless scalar value in this context. do simply returns the value of the last statement executed and so can be used to initialise a local variable.

    Rather than using a regex, this program just calls split to collect the non-whitespace fields in the file. It then checks that there were three and that they were all numeric.

    Putting the result of the split into an array avoids the problem that the captured regex fields were being lost.

    The regular expression $re is built, allowing the first two fields to appear in either order, and then grep is called on each hash element to verify whether any of the values in the hash value arrays match this file entry.

    The output seems minimal, but it contains the same information as the original code displayed.

    use strict;
    use warnings;
    
    my %data = do 'hash.pl';
    
    open my $fh, '<', 'input.txt' or die $!;
    
    while (<$fh>) {
    
      my @values = split;
      next if grep /\D/, @values or @values != 3;
    
      my $re = qr/\A$values[0]\s+$values[1]\z|\A$values[1]\s+$values[0]\z/;
    
      foreach my $key (keys %data) {
        print "$key - $values[2]\n" if grep $_ =~ $re, @{$data{$key}};
      }
    }
    

    output

    A - 4
    B - 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a group of files with the following naming convention: datetime_restofname.txt an example of
Given a folder structure: parentFolder - ChildFolder1 - somefiletolookfor.txt - (other files and folder)
I have (anonymized) a hash constructed from values in Python 2.6.8: sys.stderr.write('#' + str(dictionary['Field
Possible Duplicate: Hash SHA1 large files (over 2gb) in C# I have a large
I was given MATLAB figure files with four subplots. The last subplot has two
I want to know the files a given process is reading/writing. Is there a
How can I get all sub-directories of a given directory without files, . (current
Given the following ResourceBundle properties files: messages.properties messages_en.properties messages_es.properties messages_{some locale}.properties Note: messages.properties contains
Given an HTML page I would like to get all the 'x' files that
Trying to extract files to a given folder ignoring the path in the zipfile

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.