On Aug 15, 2006, at 6:19 PM, SFantar wrote:

I know the grep function.
Normally, grep needs a pattern to match with values found in @array.
All the values found are returned in an array.
Is that right?

Not quite.

The elements of the array are passed to grep's block, one at a time, and the block is just expected to return some value to be interpreted as boolean. Whether the value is related to the element or not it does not matter, grep just takes it an applies its logic: If the value returned by the block is true the element passes, otherwise the element is filtered out.

So the first time 'smith' is seen the code creates an entry for it in %seen and sets $is_new to 1, which is the value returned, and true. Therefore, the first time we see 'smith' it passes to @uniq. Next time 'smith' is seen in @array $is_new ends up being 0 because exists $seen{smith} is true. Now the returned value--- $is_new--- it is false, so that second 'smith' does not pass. In subsequent 'smith's the same reasoning applies, none of them pass.

Note that %seen is shared among invocations of the block because it was declared outside.

-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to