<?xml version="1.0" encoding="windows-1252"?>
<node id="960267" title="Re^2: Error in my Regular expression pattern" created="2012-03-18 03:28:52" updated="2012-03-18 03:28:52">
<type id="11">
note</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
Like [doc://perlop]#[doc://perlop#m/PATTERN/msixpodualgc] ( 
[doc://perlop]#[doc://perlop#m//] ), says, &lt;blockquote&gt;m// in scalar context returns true if it succeeds, false if it fails. &lt;/blockquote&gt;
&lt;p&gt; Each time m// succeeds against a variable, like when &lt;c&gt; $n = m/twinkle/ig; &lt;/c&gt; executes, [doc://pos]ition is changed, so that &lt;c&gt; while(/twinkle/ig) { &lt;/c&gt; executes, only the second twinkle is counted.
&lt;p&gt; If you add this to your program before the while loop &lt;c&gt; print "pos is ", pos, ", remainder '", substr($_, pos), "'\n";
pos($_) = undef;&lt;/c&gt;  this resets pos associated with $_, and then your program will produce
&lt;c&gt;Number of occurences = 1
pos is 7, remainder ' twinkle little star'
Found 2 times&lt;/c&gt;
&lt;p&gt; Like perlfaq4 says, if you want the number of occurances, use [id://815606|the flying lentil operator], use
&lt;C&gt; my $count =()= m/twinkle/ig;
&lt;/c&gt;</field>
<field name="root_node">
960264</field>
<field name="parent_node">
960266</field>
<field name="reputation">
16</field>
</data>
</node>
