sweetpea1221
sweetpea1221

Reputation: 1

Perl finding duplicates (polishing up the existing code)

Hey All below is my code:

#!/bin/usr/perl

@words = ();
@dup = ();

print "Please enter your query sentence:\n";
$input = <STDIN>;
chomp $input;

@words = split(/ /, $input);
@sort  = sort { "\L$a" cmp "\L$b" } @words;
for $n ( 0..$#sort ) {
   if (lc $sort[$n] eq lc $sort[($n+1)]) {
      push (@dup, $sort[$n]);
   }
   else {
      $n+=1;
   }
 }

 if ( @dup == () ) { 
    print "There are no duplicates in the query sentence.\n";
 }
 else {
    print "@dup \n";
 }

The problem I am having is if the word appears more than twice it appears in @dup for each additional occurrence. How do I fix this? Thanks in advance for your help!

Upvotes: 0

Views: 117

Answers (3)

anurag
anurag

Reputation: 202

Use this:

#!/bin/usr/perl
@words = ();
@dup = ();
print "Please enter your query sentence:\n";
$input = <STDIN>;
chomp $input;
@words = split(/ /, $input);
@sort= sort {"\L$a" cmp "\L$b"}@words;
%hash;
for $n (0..$#sort) {
        if (lc $sort[$n] eq lc $sort[($n+1)]) {
                if(!defined $hash{$sort[$n]}) {
                    push (@dup, $sort[$n]);
                    $hash{$sort[$n]}=1;
                }
        }
        else {$n+=1;}
            }
if (@dup==()){print "There are no duplicates in the query sentence.\n";}
else {print "@dup \n";}

just defined an additional hash %hash to keep the unique entries in the array.

Upvotes: 0

Andrey
Andrey

Reputation: 1818

print "Please enter your query sentence:\n";
$input = <STDIN>;
chomp $input;
my @words = split /\s+/, $input;
my %words;
foreach my $word(@words) {
    $words{lc($word)}++;
}
my @dup = grep {$words{$_} > 1} keys %words;
if (@dup == 0) { 
    print "There are no duplicates in the query sentence.\n";
 }
 else {
    print "@dup \n";
 }

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61512

It would be much simpler to use a Hash, which will still allow you to track duplicates and saves you from needing to presort your Array:

use strict;
use warnings; 

print "Please enter your query sentence:\n";
my $input = <STDIN>;
chomp $input;
my @words = split /\s+/, $input;

my %unique_elems;
for my $word ( @words ) {
   $unique_elems{$word}++;
}

if ( scalar keys %unique_elems == scalar @words ) { 
   print "There are no duplicates in the query sentence.\n";
}
else {
   my @dups = grep { $unique_elem{$_} > 1 } keys %unique_elems;  
   print join ',', @dups;
   print "\n";
}

A couple notes:

  1. You should always use strict; use warnings; at the top of your scripts, it will save you more time than you would think
  2. You should be using my to declare lexical variables instead of declaring globals

Upvotes: 1

Related Questions