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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:10:17+00:00 2026-06-05T01:10:17+00:00

Using Perl, I want to print only the repeating (duplicate) values in each list.

  • 0

Using Perl, I want to print only the repeating (duplicate) values in each list. The values should appear at least 2 times (2 or more times). Each list (row) should be considered separately.

For example, given the input:

abc 51082 6457 31072 5575 5488 4842 16567 151

cde 5575 3674 8150  5575 3674 8150

fgh 5737 6887 48278 3674 34399 3674 8150

I want the following output:

abc 
cde 5575 3674 8150
fgh 3674

I wrote the following source code, but it’s not giving the correct output:

#!/usr/bin/perl -w

open FH, "input.txt";
@a=<FH>;

my %count_of;

foreach $tmp (@a) 
{
    foreach $word (split /\s/, $tmp) 
    {
        $count_of{$word}++;

        if ($count_of{$word} >=2)
        {
            print "$word\n";
        }
    }
}

exit;

Could someone please guide me on what changes need to be made to the code?
Thank you!

  • 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-05T01:10:19+00:00Added an answer on June 5, 2026 at 1:10 am

    Here a working version. Look at the comments in the code to understand the corrections

    #!/usr/bin/perl
    
    # always use strict and warnings: this will help you to find errors
    use warnings;
    use strict;
    
    open FH, 'input.txt';
    
    # always declare variables
    my @a=<FH>;
    
    # you should close file handles
    close FH;
    
    # declare $tmp
    foreach my $tmp (@a) {
    
        # you need to process the first element differently since you
        # want to always print it
        my @row = split /\s/, $tmp;
    
        # you should check for empty rows
        if (@row == 0) {
            # empty row
            next;
        }
    
        # removes the first element
        my $header = shift @row;
    
        # the first element is always printed
        print $header;
    
        # this should be local otherwise you are counting globally
        # a new declaration will give you a new hash
        my %count_of;
    
        # declare $word
        foreach my $word ( @row ) {
    
            # you should not increment an undefined variable
            if ( $count_of{$word} ) {
                $count_of{$word} = 1;
            } else {
                $count_of{$word}++;
            }
    
            # just print the second time you see the word
            if ( $count_of{$word} == 2) {
                print " $word";
            }
    
        }
    
        print "\n";
    
    }
    
    # it is good practice to return true at the end of a script (no need to exit)
    1;
    

    This produces:

    abc
    cde 5575 3674 8150
    fgh 3674
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove n characters from each line using PERL. For example, I
I'm using REST::Client perl module to test my REST server. I want to print
I've created some business classes using OO Perl and I want to make sure
I'm using Perl's Calendar module to create a date, and I want to serialize
I have some data generated in MATLAB that I want to process using Perl.
I want to profile my Perl code by using Devel::DProf . when I am
I want to parse an XML file using Perl . I was able to
Using this function: perl -e 'use Time::Local; print timelocal(00,00,00,01,01,2000),\n;' It will return an epochtime
If I want to find a keyword in a sentence using Perl I have
Using Perl, how do I check if a particular Windows process is running or

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.