in reply to Grep is changing the original list?

From grep:
Note that, because $_ is a reference into the list value, it can be used to modify the elements of the array.
Why would grep work this way? Well, both map and grep are doing the same sort of thing: take a list, do some processing, and put something into a new array. With map, it puts the result from the processing, and for grep it puts the current item if the result is not false. So both probably share the same bits of code to do this.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
  • Comment on Re: Grep is changing the original list?

Replies are listed 'Best First'.
Re: Re: Grep is changing the original list?
by merlyn (Sage) on Apr 17, 2001 at 20:50 UTC
    Well, I'd venture instead that it's because it's that way for foreach, and it's that way in foreach so that we can stay efficient. Better a reference than a copy.

    -- Randal L. Schwartz, Perl hacker