Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: unique words

by sn1987a (Deacon)
on Mar 06, 2015 at 14:13 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1119033]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found