On Sep 22, Bakken, Luke said: >$/ = ''; print for grep [ chomp, s/\n\s+/ /g, s/\z/\n\n/ ], <>; > >I've never seen grep used with [ ] brackets before - it works the same >with { }: > >print for grep { chomp, s/\n\s+/ /g, s/\z/\n\n/ } <>;
Not really. Here's an example: print grep [ rand(2) < 1 ], 1 .. 10; vs. print grep { rand(2) < 1 } 1 .. 10; The first code prints all ten numbers. The second code prints roughly half of them. Why? Because [...] constructs a reference to an anonymous array, and any reference is true in a boolean context. Therefore, the code that makes up the single element in the array (rand(2) < 1) is executed, and the reference is returned to grep() for true/false. Since all references are true, all numbers 1 through 10 pass through the filter. We just have the benefit of executing that code on all 10 elements, and it doesn't matter whether rand(2) is less than 1 or not. In the SECOND code, however, rand(2) < 1 IS the true/false test. Basically, if you want to write a "filter" that executes code on each element and returns the element (possibly modified) regardless of what the return value of the filter would be, you can do: grep [ ... ], LIST # or perhaps grep [ do {...} ], LIST # or grep { ...; 1; } LIST -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]