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 8522065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:00:22+00:00 2026-06-11T07:00:22+00:00

For the Perl code below, I need to increase its efficiency since it’s taking

  • 0

For the Perl code below, I need to increase its efficiency since it’s taking hours to process the input files (which contain millions of lines of data). Any ideas on how I can speed things up?

Given two files, I want to compare the data and print those lines that match and those that don’t. Please note that two columns need to be compared interchangeably.

For example,

input1.txt
A B
C D

input2.txt
B A
C D
E F
G H

Please note:
Lines 1 and 2 match (interchangeably); Lines 3 and 4 don’t match

Output:
B A match
C D match
E F don't match
G H don't match

Perl code:

#!/usr/bin/perl -w
use strict;
use warnings;

open INFH1, "<input1.txt" || die "Error\n";
open INFH2, "<input2.txt" || die "Error\n";
chomp (my @array=<INFH2>);

while (<INFH1>) 
{

  my @values = split;
  next if grep /\D/, @values or @values != 2;

  my $re = qr/\A$values[0]\s+$values[1]\z|\A$values[1]\s+$values[0]\z/;

    foreach my $temp (@array)
    {
    chomp $_;
    print "$_\n" if grep $_ =~ $re, $temp;                      
    }
}
close INFH1;
close INFH2;
1;

Any ideas on how to increase the efficiency of this code is highly appreciated. Thanks!

  • 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-11T07:00:24+00:00Added an answer on June 11, 2026 at 7:00 am

    If you have enough memory, use a hash. If symbols do not occur multiple times in input1.txt (i.e. if A B is in the file, A X is not), the following should work:

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    my %hash;
    
    open my $F1, '<', 'input1.txt' or die $!;
    while (<$F1>) {
        my @values = split / /;
        @hash{@values} = reverse @values;
    }
    close $F1;
    
    open my $F2, '<', 'input2.txt' or die $!;
    while (<$F2>) {
        my @values = split / /;
        my $value = $hash{$values[0]};
        if ($value and $value eq $values[1]) {
            print "Matches: $_";
        } else {
            print "Does not match: $_";
        }
    }
    close $F2;
    

    Update:

    For repeated values, I would use a hash of hashes. Just sort the symbols, the first one will be the key in the large hash, the second one will be the key in the subhash:

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    my %hash;
    
    open my $IN1, '<', 'input1.txt' or die $!;
    while (<$IN1>) {
        my @values = sort split;
        undef $hash{$values[0]}{$values[1]};
    }
    close $IN1;
    
    open my $IN2, '<', 'input2.txt' or die $!;
    while (<$IN2>) {
        chomp;
        my @values = sort split;
        if (exists $hash{$values[0]}{$values[1]}) {
            print "$_ matches\n";
        } else {
            print "$_ doesn't match\n";
        }
    }
    close $IN2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create Perl code which allows counting paragraphs in text files. I
HI. I created two files 'hello.pl' and 'hello.cgi' with the code below. #!/usr/bin/perl print
I have this perl code below, basically want to loop through these 2 files.
Iam a perl newbie and need help in understanding the below piece of code.
I need a perl/shell script which will replace all the occurences of below HTML
Could you please have a look at my code below. #!C:\Perl\bin\perl.exe use strict; use
I am just learning Perl's comparison operators. I tried the below code :- $foo=291;
This code triggers the complaint below: #!/usr/bin/perl use strict; use warnings; my $s =
I need help to write some Perl code to replace some selected values in
I'm trying to decrypt a Perl code which I'm not familiar with, somehow related

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.