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!
Here a working version. Look at the comments in the code to understand the corrections
This produces: