in reply to Re: Ludicrously Stupid Foreach Loop Question
in thread Ludicrously Stupid Foreach Loop Question

ah, oops, my point exactly (blush). Small point:
if (exists($stock{$name})) { $stock{$name} += $stock; $shown{$name} += $shown; } else { $stock{$name} = $stock; $shown{$name} = $shown; }
You don't need to check it exists:
$stock{$name} += ($stock + $shown);
will do.

cLive ;-)

Replies are listed 'Best First'.
Re: Re: Ludicrously Stupid Foreach Loop Question
by pjf (Curate) on Oct 09, 2001 at 09:55 UTC
    G'day cLive,

    The code that you've given above is slightly different from what I used in my example. I was keeping stock and shown seperated, whereas your code is adding them together. I was uncertain which the original poster required, so I erred on the side of caution and went with the most flexible result.

    My original post mentions that checking for the existance of hash entries is intentional, in order to avoid warnings. However after checking with perl 5.6.1 and 5.005_03, += does not produce a warning if used with an undefined lvalue, so you're right, it is un-necessary even for avoiding warnings.

    Thanks for the feedback, I'll update my original post accordingly.

    Cheers,
    Paul