<p>When I am running the following statement:</p>

<pre><code>@filtered = map {s/&amp;nbsp;//g} @outdata;
</code></pre>

<p>it is returning an empty list instead of the filtered list that I expected.  What I am trying to do is remove every occurrence of <code>&amp;nbsp;</code> from an array of string (which is an XML file).</p>

<p>Obviously, I am not understanding something.  Can anyone tell me the correct way to do this might be, and why this isn't working for me as is?</p>

[{'answer_id': 12108, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 4, 'selected': True, 'text': '<p>Try this:</p>\n\n<pre><code>@filtered = map {s/&amp;nbsp;//g; $_} @outdata;\n</code></pre>\n\n<p>The problem is the s operator in perl modifies $_ but actually returns the number of changes it made. So, the extra $_ at the end causes perl to return the modified string for each element of @outdata.</p>\n'}, {'answer_id': 13685, 'author': 'Cebjyre', 'author_id': 1612, 'author_profile': 'https://Stackoverflow.com/users/1612', 'pm_score': 2, 'selected': False, 'text': "<p>As a counterpoint to Greg's answer, you could misuse grep:</p>\n\n<pre><code>@filtered = grep {s/&amp;nbsp;//g; 1} @outdata;\n</code></pre>\n\n<p><strong>Don't do this.</strong></p>\n"}, {'answer_id': 21792, 'author': 'Tithonium', 'author_id': 2425, 'author_profile': 'https://Stackoverflow.com/users/2425', 'pm_score': 4, 'selected': False, 'text': '<p>Note that map is going to modify your source array as well. So you could either do:</p>\n\n<pre><code>map {s/&amp;nbsp;//g} @outdata;\n</code></pre>\n\n<p>and skip the @filtered variable altogether, or if you need to retain the originals,</p>\n\n<pre><code>@filtered = @outdata;\nmap {s/&amp;nbsp;//g} @filtered;\n</code></pre>\n\n<p>Although, in that case, it might be more readable to use foreach:</p>\n\n<pre><code>s/&amp;nbsp;//g foreach @filtered;\n</code></pre>\n'}, {'answer_id': 63167, 'author': 'Michael Cramer', 'author_id': 1496728, 'author_profile': 'https://Stackoverflow.com/users/1496728', 'pm_score': 3, 'selected': False, 'text': '<p>To follow up on Tithonium\'s point, this will also do the trick:</p>\n\n<pre><code>@filtered = map {local $_=$_; s/&amp;nbsp;//g; $_} @outdata;\n</code></pre>\n\n<p>The "local" ensures you\'re working on a copy, not the original. </p>\n'}, {'answer_id': 63314, 'author': 'Shlomi Fish', 'author_id': 7709, 'author_profile': 'https://Stackoverflow.com/users/7709', 'pm_score': 3, 'selected': False, 'text': "<p>Greg's answer has the problem that it will modify the original array as the $_ are passed aliased. You need:</p>\n\n<pre><code>@filtered = map { (my $new = $_) =~ s/&amp;nbsp;//g; $new} @outdata;\n</code></pre>\n"}, {'answer_id': 329969, 'author': 'ysth', 'author_id': 17389, 'author_profile': 'https://Stackoverflow.com/users/17389', 'pm_score': 2, 'selected': False, 'text': '<pre><code>use Algorithm::Loops "Filter";\n@filtered = Filter { s/&amp;nbsp;//g } @outdata;\n</code></pre>\n'}, {'answer_id': 10657173, 'author': 'pasja', 'author_id': 944375, 'author_profile': 'https://Stackoverflow.com/users/944375', 'pm_score': 3, 'selected': False, 'text': '<p>In perl 5.14 you could use the <em>/r</em> regex modifier to <a href="http://perldoc.perl.org/perl5140delta.html#Regular-Expressions">make non-destructive substitution</a>.</p>\n\n<pre><code>@filtered = map {s/&amp;nbsp;//gr} @outdata;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/12103', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/274/']