in reply to Re: why such an error?
in thread why such an error?

Thank you for your answer. Other than using the magic "++". Could we have another method to define the $dup{$row[3]}? Thanks.

Replies are listed 'Best First'.
Re^3: why such an error?
by GrandFather (Saint) on Sep 25, 2008 at 23:24 UTC

    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
Re^3: why such an error?
by rovf (Priest) on Sep 26, 2008 at 08:59 UTC

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

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

    -- 
    Ronald Fischer <ynnor@mm.st>