in reply to why such an error?

++ is magic in this regard. See Auto increment and decrement in perlop:
undef is always treated as numeric, and in particular is changed to 0 before incrementing (so that a post-increment of an undef value will return 0 rather than undef).

Replies are listed 'Best First'.
Re^2: why such an error?
by lightoverhead (Pilgrim) on Sep 25, 2008 at 20:30 UTC
    Thank you for your answer. Other than using the magic "++". Could we have another method to define the $dup{$row[3]}? Thanks.

      There are a number of ways you could do it:

      $dup{$row[3]} = $dup{$row[3]} + 1; # Has trouble with undef $dup{$row[3]} += 1; ++$dup{$row[3]}; $dup{$row[3]}++;

      += and ++ are magical to the extent that they can update an undef value without complaint. That is often very useful, especially where a hash or array element is autovivified. For example, if you are using a hash to count the number of occurrences of words in a string you could:

      ++$wordCount{$_} for split ' ', $string;

      which generates an entry in %wordHash for each word and increments the count - very clear and concise.


      Perl reduces RSI - it saves typing

      You seem to have an inclination for dyadic plus ;-)

      $dup{$row[3]} = ($dup{$row[3]}||0) + 1;

      -- 
      Ronald Fischer <ynnor@mm.st>