Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: unique words

by sn1987a (Chaplain)
on Mar 06, 2015 at 14:13 UTC ( #1119033=note: print w/replies, xml ) Need Help??


in reply to unique words

The code the OP provides is suggestive of an approach that works if the word list is sorted.

#!/usr/bin/perl use strict; use warnings; my $num_duplicates = 0; my $last = ''; while (<DATA>) { chomp; if ( $_ eq $last ) { $num_duplicates++; } else { print "$_\n"; $last = $_; } } print "There where $num_duplicates duplicates.\n"; __DATA__ apple banana banana cherry date date date elderberry

Output:

apple banana cherry date elderberry There where 3 duplicates.

Since the OP had the count variable, I kept it to track the total number of duplicates in the list.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1119033]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2019-04-25 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (127 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!