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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:09:26+00:00 2026-06-04T12:09:26+00:00

I want to compare two text files that i have generated from one of

  • 0

I want to compare two text files that i have generated from one of the perl script that i wrote.
I want to print out the matched results from those two text files. I tried looking at couple of answers and questions that people have asked on stackoverflow but it does not work for me. Here is what i have tried.

my $file1 = "Scan1.txt";
my $file2 = "Scan2.txt";
my $OUTPUT = "final_result.txt";
my %results = (); 
open FILE1, "$file1" or die "Could not open $file1 \n";
   while(my $matchLine = <FILE1>)
       {   
         $results{$matchLine} = 1;
    }
    close(FILE1); 
    open FILE2, "$file2" or die "Could not open $file2 \n";
   while(my $matchLine =<FILE2>) 
        {  
    $results{$matchLine}++;
        }
    close(FILE2);  
    open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";
    foreach my $matchLine (keys %results) { 
    print OUTPUT $matchLine if $results{$matchLine} ne 1;
    }
    close OUTPUT;

EXAPLE OF OUTPUT THAT I WANT

FILE1.TXT
data 1
data 2
data 3

FILE2.TXT
data2
data1

OUTPUT
data 1
data 2

  • 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-04T12:09:27+00:00Added an answer on June 4, 2026 at 12:09 pm

    Your problem is that your hash now has following states:

    • 0 (line not found anywhere),
    • 1 (line found in file1 OR line found once in file2),
    • 2 (line found in file1 and once in file2, OR line found twice in file2)
    • n (line found in file1 and n-1 times in file2, OR line found n times in file2)

    This ambiguity will make your check (hash ne 1) fail.

    The minimal required change to your algorithm would be:

    my $file1 = "Scan1.txt";
    my $file2 = "Scan2.txt";
    my $OUTPUT = "final_result.txt";
    my %results = (); 
    open FILE1, "$file1" or die "Could not open $file1 \n";
       while(my $matchLine = <FILE1>)
           {   
             $results{$matchLine} = 1;
        }
        close(FILE1); 
        open FILE2, "$file2" or die "Could not open $file2 \n";
       while(my $matchLine =<FILE2>) 
            {  
        $results{$matchLine} = 2 if $results{$matchLine}; #Only when already found in file1
            }
        close(FILE2);  
        open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";
        foreach my $matchLine (keys %results) { 
        print OUTPUT $matchLine if $results{$matchLine} ne 1;
        }
        close OUTPUT;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to append two text files together. I have one file with a
So I have two files, one that contains my text, and another which I
I want to compare two arrays, one coming from a shoppingcart and the other
I want to compare two files (one file is located on local location and
Suppose I have two text files. The first one, called reference.txt has the following
I have an asp page (Default.aspx) that displays the diff between two text files.
Say I have two large (text) files which are allegedly identical, but I want
I want to be able to compare two text string and display the difference
I want to compare two CSV files to see if they are different. I
I want to compare two times in VB.net: I have 1:42:21 PM and I

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.