in reply to Use of grep with arrays/hashes

The preferred syntax for this is:
$fields{$_}++ for @fields;
Less typing, less wear-and-tear on the Perl.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Use of grep with arrays/hashes
by AidanLee (Chaplain) on May 24, 2001 at 02:57 UTC

    The reason for this, if I may venture a theory, is that using grep to loop through an array is contrary to grep's desired use. grep is used to return an array of matches from a supplied array of candidates.

    The example posted uses grep for it's side effects (ie, executing code in the grep BLOCK that has nothing to do with testing the validity of the candidate elements in the array.

Re: Re: Use of grep with arrays/hashes
by tenya (Beadle) on May 24, 2001 at 07:50 UTC
    but why do you need ++ since even undef does not remove a key?