# As suggested by toolic above use strict; use warnings; print "Please enter the file name you want the word count\n\n"; my $file= <STDIN>; chomp $file; # Lexical file handles (my $fh) are better than file globs. # Also, three-argument open() is better than two-argument open(). open(my $fh, "<", $file) or die $!; my %u_wc; my $words = 0; # Don't forget to declare $word! while (<$fh>){ my @words = split; $words += @words; $u_wc{$_}++ for @words; } # print ("The number of words in the file is $words\n\n"); # The parens are useless here, and also inconsistent with the print st +atement # that prompted us for a file name. print "The number of words in the file is $words\n\n"; # print ("this are the unique words $u_wc\n\n"); # $u_wc isn't a variable. We do have %u_wc though. print "These are the unique words: ", join(" ", keys %u_wc), "\n\n";

Output:

G:\abyss>perl x.pl Please enter the file name you want the word count x.pl The number of words in the file is 150 These are the unique words: want you file that useless my print parens + statement better $file= suggested also unique strict; $file; name words: warnin +gs; "These with and number of do += die is %u_wc 0; to have open(my $u_wc "The % +u_wc), her e, $!; <STDIN>; # "\n\n"; Lexical isn't $words\n\n"; $file) "<", $u_wc +\n\n"); } two-argument the variable. open() a = @words toolic ("The @words; or i +n As split ; $u_wc{$_}++ name. $words\n\n"); $words for enter by prompted inconsi +stent We w ord declare %u_wc; The $fh) Don't $word! join(" are globs. forget word +s keys ("t his handles count\n\n"; use above us ", though. open(). than $fh, (<$f +h>){ (my w hile chomp Also, "Please three-argument G:\abyss>

In reply to Re^3: Sorting Unique by muba
in thread Sorting Unique by rolandomantilla

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.