in reply to Re: using map with compound code blocks to alter strings: a strange result
in thread using map with compound code blocks to alter strings: a strange result
The problem with this is that the OP said that the desired result is a list that includes every element in the original list. Some will be transformed and some won't.
Another problem is more subtle. When you grep first and then map, you loop over the list twice. If it's a very long list, this will often be a waste of time. The only time I'd want to do it is if I expect grep is going to remove a lot of the list and/or the map's work is especially time consuming. In most other cases, it's better to make the decisions in the map only.
Even when you want to remove elements, you can have the map block return () for the elements to remove. The code you wrote could look like this:
my @strings = qw(boy bird FALSE); @strings = map { /FALSE/ ? qq{"$_"} : () } @strings; print join q{,}, @strings;
It does the same thing, but it's only one loop.
|
|---|