Both nvivek and jwkrahn gave you good tips on correcting your code while staying with your algorithm. However, I had a different route to get the character counts in a file. Instead of breaking the data down into words and then breaking it down further into characters, I say break down the data into the characters from the start.

I'll give you a hint at what I'm thinking about. Consider the following lines of code:

my $line = "This is sample data simulating a line from a file."; my (@chars) = ($line =~ m/([st])/gi);

What you'll end up with is an array whose elements are [T s s s t s t], which are the s's and t's from the variable $line. If you combine that with a hash, you should be able to accomplish what you want to do.

Since you said that this was an assignment, this sounds like something you're doing for a class. That's why I'm just giving hints rather than saying "Here's the code to do your assignment.", which won't be much help for future assignments and tests.

If you really, really want to see code, check out my scratchpad. Just keep in mind that you copy my stuff verbatim, your teacher/instructor will probably realize that it's not your code since it won't match your code style and might use stuff that might not have been covered yet.


In reply to Re: Hash to count characters by dasgar
in thread Hash to count characters by amittleider

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.