Others have already pointed you in the direction of the usual Perl way to solve the problem - use a hash, and have actually shown you some good coding habits along the way. However it's worth being a little more explicit about some of those habits. Consider:

use strict; use warnings; my $filename = 'delme.txt'; # Create a sample file open my $fOut, '>', $filename or die "Can't create $filename: $!\n"; print $fOut <<SAMPLE; the the the and me ok big dog me SAMPLE close $fOut; my %words = (' word ' => 'count'); open my $fIn, '<', $filename or die "Can't open $filename: $!\n"; while (<$fIn>) { $words{$_}++ for split; } close $fIn; printf "%-10s %3s\n", $_, $words{$_} for sort keys %words;

Prints:

word count and 1 big 1 dog 1 me 2 ok 1 the 3

For note the use strict: always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

Then note the use of the three parameter version of open. In particular note the '<' to make the file open mode explicit. That makes the code clearer and safer. Also note the use of lexical file handled (declared using my), that also makes the code safer.

The split looks like absolute magic, but it is simply using defaults for all its parameters. Read teh split documentation until you understand ghow it works. Note that while (<$fIn>) sets the default variable ($_) and does a little other magic so you may want to read the while documentation too to understand what's going on there.

Most of the build in functions don't need () and I tend to skip them to reduce clutter, but that means you need to use the low priority or instead of || so the die does the right thing.

Notice that the die message gives both the file name and the system error (that's what the $! special variable is about) to make it easier to diagnose file errors.

The last line uses for as a statement modifier to compactly print out the contents of the words hash. Note that the header is generated by priming the words hash in a sneaky fashion: the spaces in the key guarantee there are no conflicts with words from the text and that the header line sorts first and thus gets printed first (that's just a trick, not a "coding habit" of course).

True laziness is hard work

In reply to Re: Counting frequency of strings in files by GrandFather
in thread Counting frequency of strings in files by Cian

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.