note
johngg
<Blockquote><i><code>my @odds = map { $_ % 2 != 0 } @array;</code></i></blockquote>
<p>That's not going to do what you think. Consider the following:-</p>
<code>
johngg@abouriou:~$ perl -Mstrict -Mwarnings -E '
say for map { $_ % 2 != 0 } 0 .. 10;'
1
1
1
1
1
</code>
<p>The alternating blanks and ones are registering the FALSE and TRUE results of the expression in the [doc://map], which essentially has a one to one relation between input on the right and output on the left. What you should be using instead is [doc://grep] which filters input, only passing to the left those items for which the expression is TRUE.</p>
<code>
johngg@abouriou:~$ perl -Mstrict -Mwarnings -E '
say for grep { $_ % 2 != 0 } 0 .. 10;'
1
3
5
7
9
</code>
<p>Note also that you could dispense with the <c>!= 0</c> as the expression <c>$_ % 2</c> will evaluate to either 0 or 1.</p>
<code>
johngg@abouriou:~$ perl -Mstrict -Mwarnings -E '
say for grep { $_ % 2 } 0 .. 10;'
1
3
5
7
9
</code>
<p>I hope this is helpful.</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-401112">
<p>Cheers,</p><p>JohnGG</p>
</div></div>
11134208
11134214