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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:30:17+00:00 2026-05-14T23:30:17+00:00

I’ve a problem in making a PERL program for matching the words in two

  • 0

I’ve a problem in making a PERL program for matching the words in two documents. Let’s say there are documents A and B.

So I want to delete the words in document A that’s not in the document B.

Example 1:

A: I eat pizza

B: She go to the market and eat pizza

result: eat pizza

example 2:
A: eat pizza

B: pizza eat

result:pizza
(the word order is relevant, so “eat” is deleted.)

I use Perl for the system and the sentences in each document isn’t in a big numbers so I think I won’t use SQL

And the program is a subproram for automatic essay grading for Indonesian Language (Bahasa)

Thanx,
Sorry if my question is a bit confusing. I’m really new to ‘this world’ 🙂

  • 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-05-14T23:30:17+00:00Added an answer on May 14, 2026 at 11:30 pm

    OK, I’m without access at the moment so this is not guaranteed to be 100% or even compile but should provide enough guidance:

    Solution 1: (word order does not matter)

    #!/usr/bin/perl -w
    
    use strict;
    use File::Slurp;
    
    my @B_lines = File::Slurp::read_file("B") || die "Error reading B: $!";
    my %B_words = ();
    foreach my $line (@B_lines) {
        map { $B_words{$_} = 1 } split(/\s+/, $line);
    }
    my @A_lines = File::Slurp::read_file("A") || die "Error reading A: $!";
    my @new_lines = ();
    foreach my $line (@A_lines) {
        my @B_words_only = grep { $B_words{$_} } split(/\s+/, $line);
        push @new_lines, join(" ", @B_words_only) . "\n";
    }
    File::Slurp::write_file("A_new", @new_lines) || die "Error writing A_new: $!";
    

    This should create a new file “A_new” that only contains A’s words that are in in B.

    This has a slight bug – it will replace any multiple-whitespace in file A with a single space, so

        word1        word2              word3
    

    will become

    word1 word2 word3
    

    It can be fixed but would be really annoying to do so, so I didn’t bother unless you will absolutely require that whitespace be preserved 100% correctly

    Solution 2: (word order matters BUT you can print words from file A out with no regards for preserving whitespace at all)

    #!/usr/bin/perl -w
    
    use strict;
    use File::Slurp;
    
    my @A_words = split(/\s+/gs, File::Slurp::read_file("A") || die "Error reading A:$!");
    my @B_words = split(/\s+/gs, File::Slurp::read_file("B") || die "Error reading B:$!");
    my $B_counter = 0;
    for (my $A_counter = 0; $A_counter < scalar(@A_words); ++$A_counter) {
        while ($B_counter < scalar(@B_words)
            && $B_words[$B_counter] ne $A_words[$A_counter]) {++$B_counter;}
        last if $B_counter == scalar(@B_words);
        print "$A_words[$A_counter]";
    }
    

    Solution 3 (why do we need Perl again? 🙂 )

    You can do this trivially in shell without Perl (or via system() call or backticks in parent Perl script)

    comm -12 A B | tr "\012" " " 
    

    To call this from Perl:

    my $new_text = `comm -12 A B | tr "\012" " " `;
    

    But see my last comment why this may be considered “bad Perl”… at least if you do this in a loop with very many files being iterated and care about performance.

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

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to dedicate a machine that will host the… May 15, 2026 at 8:52 pm
  • Editorial Team
    Editorial Team added an answer No. The image Default.png is part of the bundle of… May 15, 2026 at 8:52 pm
  • Editorial Team
    Editorial Team added an answer You can do this with EF 4 and Code First. May 15, 2026 at 8:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.