"Why not try it?"

Excellent advice, especially with the caveats given.

PS: the answer is "no."

Some clarification may help the OP:

There are 4 ways to increment a counter by 1:

(A) $count{$word} = $count{$word} + 1; (B) $count{$word} += 1; (C) $count{$word}++; (D) ++$count{$word};

First, note that the $ prefix is required in $word here (this was supplied in the OP’s code, but not in the OP’s question). The difference between (C) and (D) is detailed in perldoc.

The real difference arises when warnings are turned on. (A) produces warnings of the form:

Use of uninitialized value within %count in addition (+) at ... line ..., <STDIN> line 1.

whereas (B), (C), and (D) do not. In all 4 cases, Perl creates the hash entry by a process called autovivification (on which there is a useful tutorial by Uri Guttman), which creates a new hash entry with the specified key and assigns it a value of undef. The difference is that in (A), Perl initially sees you accessing this undefined hash entry in the addition on the right of the assignment and issues a warning, whereas in (B), (C), and (D), Perl is smart enough to know that the warning is not needed. But in their effects, (A), (B), (C), and (D) are the same, in that each increments by 1 the value of the %count entry which has $word as its key.

Update: SuicideJunkie (below) makes an interesting point. A good place to follow-up on it is in the thread begun by First JAPH - Spell perl in two hundred and eighty five thousand and seventy four easy steps, a 10-year-old post which turns up from time to time in Selected Best Nodes.

HTH,

Athanasius <°(((><contra mundum


In reply to Re^2: 2 newby questions by Athanasius
in thread 2 newby questions by Socrates440

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.