rasy has asked for the wisdom of the Perl Monks concerning the following question:

how to read text from the input (one word per line) and display the unique words in the input
#!/usr/bin/perl use strict; use warnings; my $cnt=0; my $input=<>; my $unique = ''; if ($unique == $input){ $cnt++; } else if($cnt<1){ print "$unique\n"; }

Replies are listed 'Best First'.
Re: unique words
by davido (Cardinal) on Mar 06, 2015 at 04:46 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: unique words
by AnomalousMonk (Archbishop) on Mar 06, 2015 at 04:47 UTC
    c:\@Work\Perl\monks\rasy>perl -wMstrict -le "use List::MoreUtils qw(uniq); ;; my @uniques = uniq map { chomp; $_; } <>; print qq{(@uniques)}; " < words.txt (foo bar bax boff food wink wank fool zink zank)

    words.txt:

    foo bar foo bax boff foo foo food wink wank fool zink zank foo


    Give a man a fish:  <%-(-(-(-<

Re: unique words
by Anonymous Monk on Mar 06, 2015 at 04:43 UTC

    Sorry, but I don't quite understand your (pseudo)code. Could you further explain what you want to do?

    In general, getting the unique lines of a file is a common task that you can find answers for easily if you Google it (just one example of many). Assuming you did that, what did you find, and did it work for you or not? If not, what problems did you have? See also How do I post a question effectively?

    If you want to learn how to write it yourself, then the best place to start is perlintro. Combine the things you learned from there (especially the section "Files and I/O") with the FAQ How can I get the unique keys from two hashes? and you should have just about everything you need.

    P.S. In the short time since you posted this node, you've edited it at least twice; it'd probably be better if you make use of the "preview" function.

Re: unique words
by sn1987a (Curate) on Mar 06, 2015 at 14:13 UTC

    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.