This may seem like a simple task, but it's fairly complex for a beginner. It involves reading lines from a file, splitting them into words, using a hash to count appearances of certain words, and printing the contents of a hash. That gives you some things to look for in whatever learning materials you're using.

If I were writing this, the first question I would ask is, "Where do the 'certain words' come from?" If they are in a file, then the first thing you need to do is open that file, read the words from it, and put them into a hash as its keys. That might mean chomping the words, or splitting lines, depending on how the words are saved in that file. Alternatively, if the certain words are being passed on the command line, you can get them from @ARGV and put them in a hash from there.

Then you can open the file you want to search in, and start reading lines. Now you have a choice: you can split each line into words, and check your hash for each word, incrementing it if it is found. Or you can loop through your hash keys, using a regex to count the number of times each hash key appears in the line, and incrementing the key's value accordingly. Which method is faster will depend on the ratio of 'certain words' to 'all words', but I would try the regex method first.

After you've read through the whole file, you need to print out the results. Loop through your hash's keys, printing the key and its value (the number of times found). You may also wish to sort on the keys or the values as part of this loop, so check out the documentation for sort as well if that's the case.

Aaron B.
Available for small or large Perl jobs; see my home node.


In reply to Re: Help with accepting inputs, wordcount, and print to a file by aaron_baugher
in thread Help with accepting inputs, wordcount, and print to a file by underoathed

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.